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
是 m2m 关联表中的额外布尔列。给定一些Users
实例current_user
, 我想要做
current_user.meetings.visible
这将检索一个集合Meetings
用户是其参与者,其中hidden
列是false
。我得到的最接近的是将以下范围添加到Meetings
class
scope :visible, joins(:meeting_participations) & MeetingParticipation.visible
The scope
确实过滤了Meetings
反对这MeetingParticipations
表,但是没有针对该表的连接/条件MeetingParticipations
相关表current_user
.
这个问题是,如果current_user
and another_user
都是某些活动的参与者Meetings
实例,一个Meetings
将为每个参与的参与者返回结果集中的记录hidden
set to false
. If current_user
has true
设置为hidden
对全部Meetings
, if another_user
是任何此类会议的参与者hidden
set to false
, those Meetings
将出现在Meetings.visible
结果集。
是否有可能有一个像我上面提到的那样的范围,它将正确地加入User
实例?如果没有,有人可以推荐一个解决方案吗?