我用谷歌搜索,阅读文档,并观看了railscast,但我无法绕过这个。 请帮忙。我在Ubuntu 13.04上运行最新的稳定Rails和Ruby 1.9.3。
我有一个User
模型和一个Event
模型。
现在,两者之间存在has_and_belongs_to_many
的关系。
但在现实生活中,User
与Event
或Lecturer
或Student
相关。
User
可以是Student
和Lecturer
,但不能用于同一事件。
Lecturer
或Student
还不是我应用中的模型,我不确定它们是否应该是单独的模型。
我需要帮助来实现这个逻辑。
我想要实现的是这样的:
Event.first.lecturers
#应该返回作为此特定事件的讲师的用户。
Event.first.students
#应该返回此特定活动的学生用户
User.first.events :as => :student
#应返回特定用户的事件
用户是学生
User.first.events :as => :lecturer
#应返回特定用户的事件
用户是讲师
我想也许has_many :through
关联可能有用,但我很困惑。
任何和所有指导赞赏。
此致
纳迪姆
答案 0 :(得分:4)
您可以创建两个联接表events_students
和events_lecturers
,并设置这样的模型
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
然后访问它:
Event.first.students
Event.first.teachers
User.first.student_events
User.first.lecturer_events
UPD。
另一种解决方案是使用has_many:through
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
我正在将STI用于EventAssignment模型,因为如果您对不同类型的分配有不同的逻辑,它会很有用。但是如果不需要它,只需在连接模型中使用另一个列名而不是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