rails Active Query with Active Record

时间:2010-11-17 04:02:54

标签: ruby-on-rails ruby-on-rails-3

聊天(身份,状态,姓名) chat_participations(id,status,user_id)

如果计算用户的chat_participations有多少是status ='unread',我想做什么

所以我有这个:

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

这样可以正常但是当有一个chat.status ='closed'

时就会中断

我只想聊聊聊天的数量是chat.status ='open

我试过了:

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

但错误了。想法?

由于

1 个答案:

答案 0 :(得分:0)

尝试将表名放在条件中。

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