你没有显示该模型的代码,所以我做了一些假设。这里有一种方法:
class Conversation < ActiveRecord::Base
belongs_to :group
has_many :conversation_participants
has_many :participants, :through => :conversation_participants,\
:source => :user
scope :unread, lambda { |user,group| includes(:group,:conversation_participants).\
where(:group_id => group.id,\
:conversation_participants => {:read => false,:user_id => user.id})
}
end
您要求“未读对话属于特定用户和组”。由于被要求的东西是一组对话,所以这是放置范围的自然地方。
编辑
我看到你想要的数量,而不是结果集。只需添加.count
的范围:
Conversation.unread(user,group).count
EDIT 2
是有可能做这样的事情 这反而得到#, current_user.unread(集团).Count中.. ?
上的用户添加一个实例方法:
def unread(group)
Conversation.unread(self,group)
end
现在,您可以拨打current_user.unread(group).count
任何一个?这个问题是否令人困惑? – AnApprentice 2011-01-28 21:24:23