Activerecord has_many :through through multiple models

后端 未结 5 1425
忘掉有多难
忘掉有多难 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条回答
  •  说谎
    说谎 (楼主)
    2020-12-31 12:49

    I found a solution after many tries. You can use a scope with param in your last has_many sentence in the User model:

    has_many :comments, -> (user) {where organiser: user.organisers}, through: :participants
    

    The "user" param represet the User object whom is calling the comments method.

提交回复
热议问题