简体   繁体   English

Rails has_and_belongs_to_many with Many

[英]Rails has_and_belongs_to_many with Many

I'm working on a Rails 4 app with complex associations and i cant figure out how to join one model to a parent. 我正在研究具有复杂关联的Rails 4应用程序,我无法弄清楚如何将一个模型加入父模型。

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 我决定通过STI走这条路,因为这使我的应用程序迁移文件更清晰,更有条理

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. 所以不可能有一个Developer本身并没有,也可以和Organization ,其拥有许多Developers ,所以我说has_and_belongs_to_many :organizationshas_and_belongs_to_many :developers创建这种关系。 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. 所以一切都在我的应用程序中工作,如果我查找User.find().account.developersUser.find().account.organizations我得到相关的记录。


Now we get to the part where i am having trouble. 现在我们到了遇到麻烦的地方。 Within my Rails app i have a model called App . 在我的Rails应用程序中,我有一个名为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. Developer可以自己创建许多Apps ,或者Developer可以在该Organization内创建和Organization并创建许多Apps ,以便属于该Organization所有Developers也可以访问。 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

/app/models/app.rb /app/models/app.rb

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

/app/models/organization.rb /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 /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

If you want a HABTM association. 如果你想要一个HABTM协会。 you need a join table. 你需要一个联接表。 In Rails 4, you can add a migration file containing this: 在Rails 4中,您可以添加包含以下内容的迁移文件:

create_join_table :developers, :organizations

Then just use: 然后使用:

Developer.find().organizations

or 要么

Organization.find().developers

If you use User.find().account . 如果您使用User.find().account you must get account's class first, then decide to use developers or organizations 你必须先获得帐户的课程,然后决定使用developersorganizations

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM