# Update\_all for an AR instance

**URL:** <https://www.ruby-forum.com/t/update-all-for-an-ar-instance/109169>\
**Category:** Rails\
**Created:** [July 12, 2007, 3:23am UTC](https://www.ruby-forum.com/t/update-all-for-an-ar-instance/109169 "2007-07-12T03:23:06Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [July 12, 2007, 3:23am UTC](https://www.ruby-forum.com/t/update-all-for-an-ar-instance/109169/1 "2007-07-12T03:23:06Z")

</div>

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=#{obj.id}” as the  
condition.

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)  
%{#{k.to\_s}=#{v}}  
end.join(", ")  
else  
attributes  
end  
self.class.update\_all update\_sql, “id=#{id}”  
end

Let me know if there is already something like this is in RoR.
