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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 1/25/07, Giant Cranes <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > 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.You can use #update_all, or wrap your batch updates in a transaction. http://rails.rubyonrails.org/classes/ActiveRecord/Base.html#M000996 -- Rick Olson http://weblog.techno-weenie.net http://mephistoblog.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Giant Cranes
2007-Jan-25 13:15 UTC
Re: Multiple record updates in batch with ActiveRecord?
Rick Olson wrote:> You can use #update_all, or wrap your batch updates in a transaction. > > http://rails.rubyonrails.org/classes/ActiveRecord/Base.html#M000996Thanks Rick, that is exactly what I''m look for. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---