Forum: Ruby on Rails Multiple record updates in batch with ActiveRecord?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Giant C. (Guest)
on 2007-01-25 13:20
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
Rick O. (Guest)
on 2007-01-25 13:47
(Received via mailing list)
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/...

--
Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com
Giant C. (Guest)
on 2007-01-25 15:15
Rick O. wrote:

> You can use #update_all, or wrap your batch updates in a transaction.
>
> http://rails.rubyonrails.org/classes/ActiveRecord/...

Thanks Rick, that is exactly what I'm look for.
This topic is locked and can not be replied to.