Scope with join on :has_many :through association

前端 未结 8 1627
孤城傲影
孤城傲影 2020-11-30 19:14
class Users < ActiveRecord::Base
  has_many :meetings, :through => :meeting_participations
  has_many :meeting_participations
end

class Meetings < ActiveRe         


        
8条回答
  •  北海茫月
    2020-11-30 19:31

    Here's a one liner:

    Meeting.joins(:meeting_participations).where(meeting_participation: { hidden: false, user_id: current_user.id })
    

    This is great because you can make a scope out of it, a function out of it, or simply call it anywhere. You can also add any more restrictions you want to the hash.

提交回复
热议问题