Activerecord has_many :through through multiple models

后端 未结 5 1447
忘掉有多难
忘掉有多难 2020-12-31 12:00

I\'m trying to access all comments from a given user with user.comments. The query is to go through two different models, which likely both return results. My r

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-31 12:53

    Since we couldn't use has_many, through here because comments come from both of organizers and participants. I just think there are 2 solutions here:

    Basically you can still change the foreign key to accept the self.id automatically with Rails here

    User.first.comments
    
    class User
      has_many :comments, -> { joins([{ organiser: :user }, { participant: :user }]) }, through: :participants, foreign_key: "users.id"
    end
    

提交回复
热议问题