gpt4 book ai didi

ruby-on-rails - Rails Associations Riddle : User, 讲师、学生和事件

转载 作者:行者123 更新时间:2023-12-04 02:56:50 25 4
gpt4 key购买 nike

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

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

然而,在现实生活中,UserEvent 相关,作为 LecturerStudent .User 可以同时是 StudentLecturer,但不能用于同一事件。LecturerStudent 还不是我应用程序中的模型,我不确定它们是否应该是单独的模型。我需要帮助来实现这个逻辑。

我想要实现的是这样的:

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

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

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

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

我在想也许 has_many :through 关联可能有效,但我很困惑。任何和所有指导表示赞赏。

问候,

纳迪姆

最佳答案

您可以创建两个连接表 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

更新。

另一种解决方案是使用 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

关于ruby-on-rails - Rails Associations Riddle : User, 讲师、学生和事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16508763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com