2010-11-15 88 views
2

我有一个模型:轨查找和更新记录

ChatStatus具有以下字段(ID,USER_ID,状态的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' 

感谢

回答

2

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

current_user.chats.where(:thread_id => 23).update_all(:status => 'read') 
+0

感谢本。像魅力一样工作。我的数据库感谢你 – AnApprentice 2010-11-15 03:07:52