3

我已经用谷歌搜索,阅读了文档,并观看了 railscast,但我无法理解这一点。请帮忙。我在 Ubuntu 13.04 上运行最新的稳定 Rails 和 Ruby 1.9.3。

我有一个User模型,一个Event模型。目前,两者之间存在has_and_belongs_to_many关系。

然而,在现实生活中, aUserEventaLecturer或 a相关Student。AUser可以同时是 aStudentLecturer,但不能用于同一个事件。 Lecturer或者Student还不是我的应用程序中的模型,我不确定它们是否应该是单独的模型。我需要帮助来实现这个逻辑。

我想要实现的是这样的:

Event.first.lecturers# 应该返回作为此特定事件讲师的用户。

Event.first.students# 应该返回此特定事件的学生用户

User.first.events :as => :student# 应该返回用户是学生的特定用户的事件

User.first.events :as => :lecturer# 应该返回用户是讲师的特定用户的事件

我在想也许一个has_many :through协会可能会起作用,但我很困惑。任何和所有的指导表示赞赏。

问候,

纳迪姆

4

1 回答 1

4

您可以像这样创建两个连接表events_studentsevents_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
于 2013-05-12T15:51:42.373 回答