Activerecord has_many :through through multiple models

后端 未结 5 1461
忘掉有多难
忘掉有多难 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 13:00

    Could you do something as simple as:

    has_many :comments, -> { joins(:participant, :organizer) }, class_name: 'Comment'
    

    This should return all comments that have a Participant or Organizer User, since Rails tends to default joins to an inner join. You may not even need that :class_name argument.

提交回复
热议问题