我有两个模型:Player和Event,它们之间有两个连接表,参与者和课程。Rails:相同两个模型之间的多个Join表
class Event
has_many :participants
has_many :players, through: :participants
has_many :lessons
has_many :players, through: :lessons
end
class Player
has_many :participants
has_many :events, through: :participants
has_many :lessons
has_many :events, through: lessons
end
并非所有事件都有教训,但所有事件都有参与者,因此我将连接表分成两部分。
问题是,如果我要做@player.events
,生成的查询将使用课程连接表而不是参与者。
有没有更好的方法来做到这一点?
编辑:下面是连接表款:
class Lesson
belongs_to :player
belongs_to :event
end
class Participant
belongs_to :player
belongs_to :event
end
好像你想教训belong_to事件。 –
谢谢。我刚刚更新了原始文件,以显示包含belongs_to事件的连接表(课程和参与者)的模型。你还有其他建议吗? – aprilone