Scope with join on :has_many :through association

前端 未结 8 1624
孤城傲影
孤城傲影 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.

    0 讨论(0)
  • 2020-11-30 19:54
    current_user.meetings.merge(MeetingParticipations.visible)
    
    0 讨论(0)
提交回复
热议问题