2012-03-01 13 views
2

私はグループのモデルhas_many機関と学校を持っています。だから何が、私がすべき明らかにこれが不可能なユーザーとのグループの直接の関連

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 

:ユーザーをしかし、私はこのような機関や学校を通じて関連付けられているすべてのユーザをしたい:私はhave_and_belongs_to_manyする私のグループを必要とするしかし

class Group 
    has_many :institutions 
    has_many :schools 
end 

行う?

+0

あなたの施設と学校のモデルはどのように定義されていますか? – shingara

答えて

1

あなたは単なるテーブルの継承を考えたことがありますので、学校は教育機関の一種ですか?

あなたの学校のクラスは、施設( "class school <機関")から継承されます。プラス、私はあなたがそれを何か他のものと呼ぶと思いますが、 "class GenericInstitution < Institution"も持っています。グループのクラスは次のようになります。

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ですが、あなたが本当にそれを必要とする場合は、(多分、「クラスinstitutional_educationに<教育」が同じことを行うことができ、あるいは周り多分他の方法

:また

、私はかなりではないの図は、ものを見ることができます。

関連する問題