Rails: Multiple Join tables between the same two models

前端 未结 1 1300
遥遥无期
遥遥无期 2021-01-23 23:06

I have two models: Player and Event with two join tables between them, participants and lessons.

class Event
    has_many :participants
    has_many :players, th         


        
相关标签:
1条回答
  • 2021-01-24 00:11

    You can use the class_name option to change the name of one of the has_many associations but still associate with the other class. In my example I am using students for players on an event through lessons, and lectures for events on a player through lessons. But what you name them can be different.

    class Event
        has_many :participants
        has_many :players, through: :participants
    
        has_many :lessons
        has_many :students, through: :lessons, class_name: "Player"
    end
    
    class Player
        has_many :participants
        has_many :events, through: :participants
    
        has_many :lessons
        has_many :lectures, through: lessons, class_name: "Event"
    end
    
    0 讨论(0)
提交回复
热议问题