Scope with join on :has_many :through association

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

    It would seem to me that it is not sensible to use a scope on Meeting for your purpose. A meeting itself has no visibility, but the participation has. So I would suggest an extension on the association within User:

    class User < ActiveRecord::Base
      has_many :meetings, :through => :meeting_participations do
        def visible
          ids = MeetingParticipation.
            select(:meeting_id).
            where(:user_id => proxy_owner.id, :visible => true).
            map{|p| p.meeting_id}
          proxy_target.where("id IN (?)", ids)
        end
      end
      ...
    end
    

    I hope, this helps.

提交回复
热议问题