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.
82c59780911721f5cbe82d3111313519?d=identicon&s=25 Giant Cranes (giantcranes)
on 2007-01-25 12: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
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-01-25 12:47
(Received via mailing list)
On 1/25/07, Giant Cranes <rails-mailing-list@andreas-s.net> 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 Olson
http://weblog.techno-weenie.net
http://mephistoblog.com
82c59780911721f5cbe82d3111313519?d=identicon&s=25 Giant Cranes (giantcranes)
on 2007-01-25 14:15
Rick Olson 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.