按条件Ruby on Rails更新记录

时间:2010-12-01 08:25:13

标签: ruby-on-rails rails-models

我们可以按条件更新记录而不先检查记录吗?

1 个答案:

答案 0 :(得分:5)

是的,方法是update_all。

# Update all books with 'Rails' in their title
Book.update_all "author = 'David'", "title LIKE '%Rails%'"

http://apidock.com/rails/ActiveRecord/Relation/update_all