named_scope in rails 2
I have two models.. Member and MemberMeeting .
Member has_many member开发者_Python百科_meetings.
I have written a named_scope in Member model such that it does a join operation on the member_meetings.
Now the hard part,
I have a boolean column xyz. I need to check for count(xyz=true) > 1 inside the named_scope
Are you sure you want a scope for this? I assume you want a list of all members that have more than one meeting where xyz is true, right? In that case, something like this should do:
members = Member.include(:member_meetings)
members.select{ |m| m.member_meetings.select(&:xyz).size > 1 }
Adding this named_scope to Member should do it:
named_scope :my_scope, :include => :member_meetings, :conditions => { 'member_meetings.xyz' => true }
精彩评论