开发者

rails Relationship Query with Active Record

开发者 https://www.devze.com 2023-01-25 05:43 出处:网络
chat (id, status, name) chat_participations (id, status, user_id) What I want to do if get a count for how many of the user\'s chat_participations are status = \'unread\'

chat (id, status, name) chat_participations (id, status, user_id)

What I want to do if get a count for how many of the user's chat_participations are status = 'unread'

So I have this:

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

That works ok but it breaks when there is a chat.status = 'closed'

I only want the count for chat's that are c开发者_如何学JAVAhat.status = ' open

I tried:

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

but that error'd. Ideas?

Thanks


Try putting the table name in the conditions.

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

精彩评论

暂无评论...
验证码 换一张
取 消