Rails 3 + activerecord, the best way to "mass update" a single field for all the records that meet a condition

Solution 1:

Use update_all with the optional second parameter for the condition:

Model.update_all({ hidden: true }, { phonenum: some_phone_number})

Solution 2:

The update_all does not allow conditions in rails 3. You can use combination of scope and update_all

Model.where(phonenum: some_phone_number).update_all(hidden: true)

Reference: http://m.onkey.org/active-record-query-interface

Solution 3:

If you want to trigger callbacks:

class ActiveRecord::Base
  def self.update_each(updates)
    find_each { |model| model.update(updates) }
  end

  def self.update_each!(updates)
    find_each { |model| model.update!(updates) }
  end
end

Then:

Model.where(phonenum: some_phone_number).update_each(hidden: true)