2012-03-01 2 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 

:

class Group 
    has_many :institutions 
    has_many :schools 
end 

그러나 나는 have_and_belongs_to_many 내 그룹이 필요합니다 사용자하지만 난 모든이 같은 기관과 학교를 통해 연결된 사용자를 원한다 해야 할 것?

+0

기관 및 학교 모델을 어떻게 정의합니까? – shingara

답변

1

그냥 테이블 상속을 사용하는 것을 고려 했으므로 학교가 교육 기관입니까?

학교 수업은 기관 ("class school < 기관")에서 상속됩니다. 게다가, 당신이 뭔가 다른 것을 부르 겠지만 "class GenericInstitution < Institution"도 가질 수 있습니다. 그런 다음 Group 클래스는 다음과 같이 표시 될 수 있습니다.

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에게 < 교육"이 같은 일을 할 수있는, 또는 주위에 어쩌면 다른 방법

: 또한

, 꽤되지 그림은 무엇을 볼 수 있습니다.