以两种不同的方式访问 has_many 关系 ActiveRecord Rails
我需要通过两种方式访问机构。 我的模型如下:
class Person < ActiveRecord::Base
has_many :institution_people
has_many :institution_choices
has_many :institutions, :through => :institution_people
has_many :institutions, :through => :institution_choices
fields........
end
class Institution < ActiveRecord::Base
has_many :people, :through => :institution_people
has_many :people, :through => :institution_choices
has_many :institution_people
has_many :institution_choices
end
class InstitutionChoice < ActiveRecord::Base
belongs_to :person
belongs_to :institution
end
class InstitutionPerson < ActiveRecord::Base
belongs_to :person
belongs_to :institution
end
我设置的模型是这样的,即人可以在不同的机构学习,因此为此我设置
has_many :institutions, :through => :institution_people
个人模型
但同时人可以有机构选择,所以我设置
has_many :institutions, :through => :institution_choices
个人模型。
我应该如何建立人与机构之间的模型和关联,以便我可以通过两种方式从人中找到机构。
现在
Person.first.institutions
从institution_people表中找到,
has_many :institutions, :through => :institution_people
我猜就像一开始一样。
欢迎其他一些技术,以便我可以通过两种方式获得机构。
I need to access institution in two ways.
My models are given below:
class Person < ActiveRecord::Base
has_many :institution_people
has_many :institution_choices
has_many :institutions, :through => :institution_people
has_many :institutions, :through => :institution_choices
fields........
end
class Institution < ActiveRecord::Base
has_many :people, :through => :institution_people
has_many :people, :through => :institution_choices
has_many :institution_people
has_many :institution_choices
end
class InstitutionChoice < ActiveRecord::Base
belongs_to :person
belongs_to :institution
end
class InstitutionPerson < ActiveRecord::Base
belongs_to :person
belongs_to :institution
end
The i setup the models like this is that person can study in different institutions, so for this i setup
has_many :institutions, :through => :institution_people
for person model
But at the same time person can have institution choices, so i setup
has_many :institutions, :through => :institution_choices
for person model.
How should i setup model and association between person and institutions so that i can find institutions from person in both ways.
Right now
Person.first.institutions
finds from institution_people table, as
has_many :institutions, :through => :institution_people
is at beginning i guess.
Some other techniques are welcomed so that i can get institutions in both ways.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在您的
Person
模型中,尝试以下操作:http:// guides.rubyonrails.org/association_basics.html#has_many-association-reference
In your
Person
model, try this :http://guides.rubyonrails.org/association_basics.html#has_many-association-reference
基本上,您在这里需要某种接口。我会做什么:
在机构模型中:(
它从下面的 Person.rb 加入查询(范围))
在 Person.rb 中:(
这应该从两个表中检索所有机构 ID)
丑陋极了,但仍然可以工作。您可以使用如下:
Institution.institutions_of(current_user)
Basically, you need some kind of interface here. What i would do:
in institutions model:
( it joins the query(scope) from Person.rb below )
in Person.rb :
(this should retrieve all institutions ids from both tables)
ugly as hell, but still might work. You could use is then like:
Institution.institutions_of(current_user)
如果您不需要将结果保留为 ActiveRelation
如果您确实需要保留它(例如为了调用 person.institutions.order('name')),那么它会更复杂:
If you don't need to keep the result as an ActiveRelation
If you DO need the keep it (in order to call person.institutions.order('name'), by example), then it's more complicated: