Multiple record updates in batch with ActiveRecord?


#1

Hi,

Does ActiveRecord cater for updating multiple records based on some
criteria without getting chatty with the database?

I am currently doing something like the following:

def renew_all_vehicles
for vehicle in
self.vehicles.find_all_by_status(VehicleStatus::FOR_SALE,
:conditions => [“renewed_on <= ?”, Time.new - 86400])
vehicle.renew() #this updates a date column and saves
end
end

I want to improve the performance of this and am going to execute a
single custom SQL statement unless there is an ActiveRecord way.

Thanks for any help,
GiantCranes


#2

On 1/25/07, Giant C. removed_email_address@domain.invalid wrote:

self.vehicles.find_all_by_status(VehicleStatus::FOR_SALE,
:conditions => [“renewed_on <= ?”, Time.new - 86400])
vehicle.renew() #this updates a date column and saves
end
end

I want to improve the performance of this and am going to execute a
single custom SQL statement unless there is an ActiveRecord way.

You can use #update_all, or wrap your batch updates in a transaction.

http://rails.rubyonrails.org/classes/ActiveRecord/Base.html#M000996


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com


#3

Rick O. wrote:

You can use #update_all, or wrap your batch updates in a transaction.

http://rails.rubyonrails.org/classes/ActiveRecord/Base.html#M000996

Thanks Rick, that is exactly what I’m look for.