class Users < ActiveRecord::Base has_many :meetings, :through => :meeting_participations has_many :meeting_participations end class Meetings < ActiveRecord::Base has_many :users, :through => :meeting_participations has_many :meeting_participations end class MeetingParticipations < ActiveRecord::Base belongs_to :user belongs_to :meeting scope :hidden, where(:hidden => true) scope :visible, where(:hidden => false) end
hidden is an optional boolean column in the m2m mapping table. Given some Users instance of current_user , I want to do
current_user.meetings.visible
which will retrieve the Meetings collection for which the user is a member, where the hidden column is false . The closest I got is adding the following scope to the Meetings class
scope :visible, joins(:meeting_participations) & MeetingParticipation.visible
scope filters Meetings according to the MeetingParticipations table, however there is no join / condition regarding the MeetingParticipations table associated with current_user .
The problem is that if current_user and another_user are participants for the Meetings instance, the Meetings record in the result set will be returned for each participant whose hidden set to false . If current_user has true for hidden for hidden for all Meetings , if another_user is a participant in any of the same meetings with hidden set to false , those Meetings will appear in the Meetings.visible result set.
Is it possible to have a scope, as I mentioned above, that will correctly join the User instance? If not, can someone recommend a solution?
ruby-on-rails ruby-on-rails-3
deefour May 2 '11 at 11:56 2011-05-02 11:56
source share