rails查找和更新记录

时间:2010-11-15 02:50:51

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

我有一个模特:

ChatStatus包含以下字段(ID,user_id,status,thread_id)

我想要做的是将用户的状态从“未读”更新为“阅读”

我要做的是:

@chat = current_user.chats.where(:thread_id => 23)

23来自AJAX帖子。然后我必须这样做:

@chat.status = 'read'
@chat.save

两次击中数据库。我想知道有没有办法在数据库中请求所有这些?类似的东西:

@chat = current_user.chats.where(:thread_id => 23).status = 'read'

由于

1 个答案:

答案 0 :(得分:2)

我认为这是您正在寻找的语法:

current_user.chats.where(:thread_id => 23).update_all(:status => 'read')
相关问题