【问题标题】:Direct association for a Group with a User组与用户的直接关联
【发布时间】:2012-03-01 08:46:51
【问题描述】:

我的 Group 模型有_许多机构和学校。

class Group
  has_many :institutions
  has_many :schools
end

但是我需要我的组拥有_and_belongs_to_many :users 但我希望通过这样的机构和学校关联的所有用户:

class Group
  has_many :institutions
  has_many :users, :through => :instiutions
  has_many :schools
  has_many :users, :through => :schools
end

class School
  belongs_to :group      

  has_many :educations
  has_many :users, :through => :educations
end

class Institution
  belongs_to :group      

  has_many :institutional_educations
  has_many :users, :through => :institutional_educations
end

显然这是不可能的,我该怎么办?

【问题讨论】:

  • 如何定义您的机构和学校模式?

标签: mysql ruby-on-rails ruby ruby-on-rails-3 associations


【解决方案1】:

顺便说一句,你有没有考虑过使用单表继承,让学校成为一种机构?

您的 School 课程将从机构继承(“class School

 class Group
  :has_many :school_users, :through => :schools, 
  :has_many :generic_institution_users, :through => :generic_institutions

   # if you need all of the users at once: 
   :has_many :users, :through => :institutions
 end

您可能必须指定一两个外键才能让它为您工作。

另外,我不太清楚 :institutional_education 是什么,但如果你真的需要它,你可以在那里做同样的事情(可能是“classstitution_education

【讨论】:

    猜你喜欢
    • 2014-03-24
    • 2012-07-04
    • 2011-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 2012-03-06
    相关资源
    最近更新 更多