[英]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
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 :organizations
和has_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.developers
或User.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
你必须先获得帐户的课程,然后决定使用developers
或organizations
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.