2010-11-17 106 views
0

聊天(ID,状态,名称) chat_participations(ID,状态,USER_ID)轨关系查询与活动记录

我想,如果得到了用户如何的chat_participations的很多都是状态计数做='未读”

所以我有这样的:

@chats_unread = current_user.chat_participations.where(:status => 'unread').count 

这工作正常,但是当有一个chat.status =它打破‘封闭’

我只W¯¯蚂蚁计数聊天的是chat.status ='开放

我想:

@chats_unread = current_user.chat.where(:status => 'open).chat_participations.where(:status => 'unread').count 

但error'd。想法?

谢谢

+2

从这里的小代码,它看起来像用户没有与聊天的关系,所以你的current_user.chat调用不起作用。另外,聊天和chat_participations之间有关联吗? – raidfive 2010-11-17 04:28:37

+0

chat_participations是否也有chat_id? – rwilliams 2010-11-17 05:29:22

+0

是您使用的确切代码?因为我可以看到缺少字符('),所以代码应该是 @chats_unread = current_user.chat.where(:status =>'open')。chat_participations.where(:status =>'unread')。count 检查它是否正确。 – 2010-11-17 07:06:57

回答

0

尝试把表名称的条件。

where('chats.status' => 'open') 
where('chat_participations.status' => 'unread')