Question

I'm working on a Rails 4 app with complex associations and i cant figure out how to join one model to a parent.

Basically my apps logic is as follows

User
  - belongs_to :account, polymorphic: true

Developer
  - has_one :user, as: :account
  - has_and_belongs_to_many :organizations

Organization
  - has_one :user, as: :account
  - has_and_belongs_to_many :developers
  • I decided to go this route over a STI because this allowed me to make my apps migration file cleaner and more organized

So there can be a Developer by itself and there can also be and Organization which holds many Developers, so i added has_and_belongs_to_many :organizations and has_and_belongs_to_many :developers to create this relationship. So everything works here in my app if i look up User.find().account.developers or User.find().account.organizations i get the associated records.


Now we get to the part where i am having trouble. Within my Rails app i have a model called App. A Developer can create many Apps by itself or a Developer can create and Organization and create many Apps within that Organization that all of the Developers who belong to that Organization have access too. I have no idea how to implement a relationship like this. Any suggestions?


Developer
  has_one :user, as: :account
  has_many :apps, as: :appable
  has_and_belongs_to_many :founding_organizations, class_name: 'Organization', foreign_key: :founder_id
  has_and_belongs_to_many :collaborating_organizations, class_name: 'Organization', foreign_key: :collaborator_id

  has_and_belongs_to_many :organizations   (Should this stay?)

Organization
       has_one :user, as: :account
  has_many :apps, as: :appable
  has_and_belongs_to_many :founders, class_name: 'Developer', association_foreign_key: :founder_id
  has_and_belongs_to_many :collaborators, class_name: 'Developer', association_foreign_key: :collaborator_id

  has_and_belongs_to_many :developers   (Should this stay?)

App
  belongs_to :appable, polymorphic: true
Was it helpful?

Solution

/app/models/app.rb

App < ActiveRecord::Base
  belongs_to :appable, :polymorphic => true
end

/app/models/organization.rb

Organization < ActiveRecord::Base
  has_and_belongs_to_many :founders, :class_name => 'Developer', :association_foreign_key => :founder_id, :join_table => 'founders_organizations'
  has_and_belongs_to_many :collaborators, :class_name => 'Developer', :association_foreign_key => :collaborator_id, :join_table => 'collaborators_organizations'
  has_many :apps, :as => :appable
  # Other relationships
 end

/app/models/developer.rb

Developer < ActiveRecord::Base
  has_and_belongs_to_many :founded_organizations, :class_name => 'Organization', :foreign_key => :founder_id, :join_table => 'founders_organizations'
  has_and_belongs_to_many :collaborated_organizations, :class_name => 'Organization', :foreign_key => :collaborator_id, :join_table => 'collaborators_organizations'
  has_many :apps, :as => :appable
  # Other relationships
 end

OTHER TIPS

If you want a HABTM association. you need a join table. In Rails 4, you can add a migration file containing this:

create_join_table :developers, :organizations

Then just use:

Developer.find().organizations

or

Organization.find().developers

If you use User.find().account. you must get account's class first, then decide to use developers or organizations

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top