Rails - How to create a model linked to TWO of another model

后端 未结 2 486
无人共我
无人共我 2021-02-03 10:49

I am trying to create the following:

User model (this is fine)

id

Link model (associated with two Users)

id
user_id1
user_id2

Is this an in

2条回答
  •  星月不相逢
    2021-02-03 11:40

    You can create A Join Model that relation between the Link between the two users models

    so basically

    
    class User
    
      has_many :links, :through => :relationships
    
    end
    
    class Relationship
    
      belongs_to :user_id_1, :class=> "User"
      belongs_to :user_id_2, :class=> "User"
    
    end
    
    

提交回复
热议问题