Scope with join on :has_many :through association

前端 未结 8 1629
孤城傲影
孤城傲影 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:52

    The clean, associations way to do it is:

    has_many :visible_meetings, -> { merge(MeetingParticipations.visible) },
      :source => :meeting, :through => :meeting_participations
    

    To put it in more generic terms: if you have a chained has_many association you can scope the intermediate (through) association via merging the scope. Probably requires Rails 4+.

    Otherwise this would have to be done via creating a (probably unwanted) intermediate scoped association as seen in @Paul Pettengill's answer.

提交回复
热议问题