Forum: Ruby on Rails update_all for an AR instance

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
525128e48ca2b4c7fb6176ea166fccfd?d=identicon&s=25 Eric G. (gotskill10)
on 2007-07-12 03:23
Is there anyway to call "update_attributes" where it updates only the
attributes you specify with the update command.

The only thing close to this is update_all where you have to specify the
condition. So essentially you'd be stuck doing "id=#{}" as the

Are there any built in AR functions that allow you to update a
particular object using straight sql UPDATE without saving all the
attributes (ie, updating only the attributes you designate.)

In any case, I extended ActiveRecord::Base to do something like this.
Here is my function:

      def update_attributes_with_sql(attributes) # allows you to update
individual columns without saving the whole record (also much faster)
         update_sql = if attributes.is_a?(Hash)
               attributes.to_a.collect do |key_value|
                  k, v = key_value
                  v = v.to_s(:db) if v.is_a?(Time)
                  v = "'#{v}'" if v.is_a?(String)
               end.join(", ")
         self.class.update_all update_sql, "id=#{id}"

Let me know if there is already something like this is in RoR.
This topic is locked and can not be replied to.