2011-05-11 2 views
0

관련된 하나 개의 테이블에 다수의 필드는 I 2 개 테이블을 가지고 사람 일괄루비, 다른 테이블

일괄 개인 테이블에 관한 3 개의 필드 : 감독, 제작자 및 개질제.

모두 3 개의 필드는 Person 테이블의 person_id를 저장합니다.

나는 사람과 배치 모델간에 다음과 같은 관계를 이미 만들었습니다. 내가 'batch.person.PER_NAME'을 할 경우

class Batch < ActiveRecord::Base 
    belongs_to :person, :foreign_key => "supervisor_id" 
end 

class Person < ActiveRecord::Base 
    has_many :batches, :foreign_key => "supervisor_id" 
end 

그래서, 나는 창조자 및 수정 사항을 얻기 위해 계속 어떻게 감독자의 name..but을받을 수 있습니까? batch.supervisor batch.creator 배치 : 그럼 당신은 독립적으로 각 하나를 참조 할 수 있습니다

class Batch < ActiveRecord::Base 
    belongs_to :supervisor 
    belongs_to :creator 
    belongs_to :modifier 
end 

: 어떤 제안에 대한

많은 감사는 3 개 가지 관계를 원하는 것 소리

답변

2

를 제공했다. 수정 자

또는 두 추가 구성 요소 사이에 조인 테이블을 만들고 임의로 추가 및 제거 할 수 있습니다.

class Batch < ActiveRecord::Base 
    has_many :person_batches 
    has_many :people, :through => :person_batches, :source => :persons # something like that 
end 

class Person < ActiveRecord::Base 
    has_many :batches, :through => :person_batches 
end 

class PersonBatches < ActiveRecord::Base 
    belongs_to :person 
    belongs_to :batch 

    # Has a column called :person_type where you can specify 
    # "supervisor," "creator," "modifier," etc. This would 
    # also allow a batch to have multiples of each, if that's 
    # useful. Otherwise, you can add validations around this. 
end