update_allメソッドって便利だなー
where()などで必要な複数レコードを抽出しておいて、一括で値変更できます。
(ActiveRecord::Relationのメソッドとして実装されてる)
いままでsql直書きしてたよ。。(´・ω・`)
その前は、eachをレコード分実行して値書き換えてたよ。よくないよね。。(´;ω;ω;`)
忘備録的にリファレンスマニュアルのサンプルを貼り付けます。
# Update all customers with the given attributes Customer.update_all :wants_email => true # Update all books with 'Rails' in their title Book.update_all "author = 'David'", "title LIKE '%Rails%'" # Update all avatars migrated more than a week ago Avatar.update_all ['migrated_at = ?', Time.now.utc], ['migrated_at > ?', 1.week.ago] # Update all books that match conditions, but limit it to 5 ordered by date Book.update_all "author = 'David'", "title LIKE '%Rails%'", :order => 'created_at', :limit => 5 # Conditions from the current relation also works Book.where('title LIKE ?', '%Rails%').update_all(:author => 'David') # The same idea applies to limit and order Book.where('title LIKE ?', '%Rails%').order(:created_at).limit(5).update_all(:author => 'David')