2

나는 googled하고, 문서를 읽고, railscast를 보았다. 그러나 나는 이것에 대해 내 머리를 감쌀 수 없다. 도와주세요. Ubuntu 13.04에서 최신 안정 레일 및 Ruby 1.9.3을 실행하고 있습니다.레일스 협회 수수께끼 : User, Lecturer, Student and Event

나는 User 모델과 Event 모델을 가지고 있습니다. 지금은 두 사람 사이에 has_and_belongs_to_many 관계가 있습니다.

실생활에서 다소 UserLecturer 또는 Student 하나로서 Event에 관한 것이다. UserStudentLecturer이 될 수 있지만 동일한 이벤트는 해당되지 않습니다. Lecturer 또는 Student은 아직 내 앱에서 모델이 아니며 별도의 모델이어야하는지 확신 할 수 없습니다. 이 논리를 구현하는 데 도움이 필요합니다.

Event.first.lecturers #이 특정 이벤트에 대한 강사 인 사용자를 반환해야합니다 : 나는 무엇을 달성하고자하는

이 같은 것입니다.

Event.first.students #이 특정 이벤트에 대한 학생 인 사용자를 반환해야 사용자가

User.first.events :as => :lecturer # 특정 사용자 위치를위한 이벤트를 반환해야 학생에게 있습니다 특정 사용자에 대한 이벤트를 반환해야

User.first.events :as => :student # 사용자가 강사 인 경우

아마도 has_many :through 연결이 작동한다고 생각하지만 너무 혼란 스럽습니다. 모든 안내가 감사합니다. 당신은 만들 수 있습니다

감사합니다,

나딤

답변

3

두이

class Event < ActiveRecord::Base 
    has_and_belongs_to_many :students, :class_name => 'User', :join_table => :events_students, :association_foreign_key => :user_id 
    has_and_belongs_to_many :lecturers, :class_name => 'User', :join_table => :events_lecturers, :association_foreign_key => :user_id 
end 

class User < ActiveRecord::Base 
    has_and_belongs_to_many :student_events, :class_name => 'Event', :join_table => :events_students, :association_foreign_key => :event_id 
    has_and_belongs_to_many :lecturer_events, :class_name => 'Event', :join_table => :events_lecturers, :association_foreign_key => :event_id 
end 

같은 테이블 events_studentsevents_lecturers 및 설정 모델에 가입 한 후 액세스 :

Event.first.students 
Event.first.teachers 
User.first.student_events 
User.first.lecturer_events 

UPD를. 이 과제의 다른 유형에 대해 서로 다른 논리가있는 경우 유용하게 사용될 수 있기 때문에 내가 EventAssignment 모델 STI를 사용하고

class User < ActiveRecord::Base 
    has_many :event_assignments 
    has_many :student_events, :through => :event_assignments, :source => :event, :conditions => {'event_assignments.type' => 'EventAssignment::Student'} 
    has_many :lecturer_events, :through => :event_assignments, :source => :event, :conditions => {'event_assignments.type' => 'EventAssignment::Lecturer'} 
end 

class Event < ActiveRecord::Base 
    has_many :event_assignments 
    has_many :students, :through => :event_assignments, :source => :user, :conditions => {'event_assignments.type' => 'EventAssignment::Student'} 
    has_many :lecturers, :through => :event_assignments, :source => :user, :conditions => {'event_assignments.type' => 'EventAssignment::Lecturer'} 
end 

class EventAssignment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :event 
end 

class EventAssignment::Student < EventAssignment 
end 

class EventAssignment::Lecturer < EventAssignment 
end 

을 통해 :

또 다른 해결책은 has_many을 사용하는 것입니다. 필요하지 않은 경우에, 다만 관계 유형을 식별하는 모델을 결합 다른 열 이름을 사용하는 대신에 type :

class User < ActiveRecord::Base 
    has_many :event_assignments 
    has_many :student_events, :through => :event_assignments, :source => :event, :conditions => {'event_assignments.kind' => 'student'} 
    has_many :lecturer_events, :through => :event_assignments, :source => :event, :conditions => {'event_assignments.kind' => 'lecturer'} 
end 

class Event < ActiveRecord::Base 
    has_many :event_assignments 
    has_many :students, :through => :event_assignments, :source => :user, :conditions => {'event_assignments.kind' => 'student'} 
    has_many :lecturers, :through => :event_assignments, :source => :user, :conditions => {'event_assignments.kind' => 'lecturer'} 
end 

class EventAssignment < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :event 
end 
+0

사과를, 나는 당신의 대답을 통보하지 않았다. 그리고 고마워! 첫 번째 해결 방법을 시도했습니다. 그러나 Event.lecturers와 Event.students 모두 동일한 결과를 반환합니다. 강사를 저축하고 있습니다 : event_in_db.lecturers << user 또한, events_students 및 events_lecturer 조인 테이블에서 user_id 및 student_id/lecture_id를 모두 열로 시도했습니다. 상관 없어. –

+0

'Event' 모델에'join_table' 옵션을 추가하는 것을 잊었 기 때문에 첫 번째 해결책이 작동하지 않는 것 같습니다 –