以两种不同的方式访问 has_many 关系 ActiveRecord Rails

发布于 2024-11-02 10:44:31 字数 1269 浏览 1 评论 0原文

我需要通过两种方式访问​​机构。 我的模型如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆橘 2024-11-09 10:44:31

在您的 Person 模型中,尝试以下操作:

class Person < ActiveRecord::Base
  has_many :institution_people
  has_many :institution_choices
  has_many :institutions_people, :through => :institution_people, :source => :institutions, :class_name => "Institution"
  has_many :institutions_choices, :through => :institution_choices, :source => :institutions, :class_name => "Institution"
end

http:// guides.rubyonrails.org/association_basics.html#has_many-association-reference

In your Person model, try this :

class Person < ActiveRecord::Base
  has_many :institution_people
  has_many :institution_choices
  has_many :institutions_people, :through => :institution_people, :source => :institutions, :class_name => "Institution"
  has_many :institutions_choices, :through => :institution_choices, :source => :institutions, :class_name => "Institution"
end

http://guides.rubyonrails.org/association_basics.html#has_many-association-reference

我爱人 2024-11-09 10:44:31

基本上,您在这里需要某种接口。我会做什么:

在机构模型中:(

scope :institutions_of, proc { |person| joins(' INNER JOIN (' + Person.institution_ids(person) + ') q
  ON institutions.id = q.ip_iid OR institutions.id = q.ic_iid').where(['institutions.person_id = ?', person_id]

它从下面的 Person.rb 加入查询(范围))

在 Person.rb 中:(

    scope :institution_ids,  proc { |person| select('ip.institution_id ip_idd, ic.institution_id ic_idd
from institution_people ip
inner join institution_choice ic on ip.person_id = ic.person_id').
where(['ip.person_id = ?', person.id])

这应该从两个表中检索所有机构 ID)

丑陋极了,但仍然可以工作。您可以使用如下: Institution.institutions_of(current_user)

Basically, you need some kind of interface here. What i would do:

in institutions model:

scope :institutions_of, proc { |person| joins(' INNER JOIN (' + Person.institution_ids(person) + ') q
  ON institutions.id = q.ip_iid OR institutions.id = q.ic_iid').where(['institutions.person_id = ?', person_id]

( it joins the query(scope) from Person.rb below )

in Person.rb :

    scope :institution_ids,  proc { |person| select('ip.institution_id ip_idd, ic.institution_id ic_idd
from institution_people ip
inner join institution_choice ic on ip.person_id = ic.person_id').
where(['ip.person_id = ?', person.id])

(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)

你与昨日 2024-11-09 10:44:31

如果您不需要将结果保留为 ActiveRelation

class Person < ActiveRecord::Base
  has_many :institution_people
  has_many :institution_choices

  def institutions
    institution_people + institution_choices
  end
end

如果您确实需要保留它(例如为了调用 person.institutions.order('name')),那么它会更复杂:

class Person < ActiveRecord::Base
  has_many :institution_people
  has_many :institution_choices

  scope :institutions, :select => 'select * from (select * from institution_people union all select * from institution_choices) as institutions'
end

If you don't need to keep the result as an ActiveRelation

class Person < ActiveRecord::Base
  has_many :institution_people
  has_many :institution_choices

  def institutions
    institution_people + institution_choices
  end
end

If you DO need the keep it (in order to call person.institutions.order('name'), by example), then it's more complicated:

class Person < ActiveRecord::Base
  has_many :institution_people
  has_many :institution_choices

  scope :institutions, :select => 'select * from (select * from institution_people union all select * from institution_choices) as institutions'
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文