# Update\_attribute send updates every field, why?

**URL:** <https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173>\
**Category:** Rails\
**Created:** [November 17, 2005, 12:54pm UTC](https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173 "2005-11-17T12:54:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [November 17, 2005, 12:54pm UTC](https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173/1 "2005-11-17T12:54:25Z")

</div>

Hi,

If you do model.update\_attribute(“fname”, “John”) the every field is  
updated, it generates a big update statement like:

update persons set fname=x, lname=y, email=z, whatever=blah,  
and\_stuff=stuff where id=1

I would think it would just generate:  
update persons set fname=‘John’ where id=1

Anybody know why AR acts this way?

Jeroen

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [November 18, 2005, 1:11pm UTC](https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173/2 "2005-11-18T13:11:13Z")

</div>

Jeroen H. wrote:

> Anybody know why AR acts this way?

I think that’s just the Rails does it. Records are always  
saved in their entirety.

I’ve found the following simple Active Record extentions to be  
helpful in cutting out some useless database activity:

class ActiveRecord::Base  
def changed(attrs)  
return false if attrs.nil?  
self.class.content\_columns.each { |c| return true if self[c] !=  
attrs[c] }  
return false  
end

def update\_attributes\_if\_changed(attrs)  
changed(attrs) ? update\_attributes(attrs) : true  
end

def update\_attribute\_if\_changed(name, value)  
self[name] != value ? update\_attribute(name, value) : true  
end  
end

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [November 18, 2005, 5:13pm UTC](https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173/3 "2005-11-18T17:13:27Z")

</div>

Mark Reginald J. wrote:

> > I would think it would just generate:
> 
> ```
> changed(attrs) ? update_attributes(attrs) : true
> 
> ```
> 
> end
> 
> def update\_attribute\_if\_changed(name, value)  
> self[name] != value ? update\_attribute(name, value) : true  
> end  
> end

This doesn’t address my original problem though, right? I mean in the  
end you still call update\_attribute which generates the full update  
SQL, not just for one field. Or am I missing something here…

Jeroen

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [November 19, 2005, 1:28am UTC](https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173/4 "2005-11-19T01:28:56Z")

</div>

Jeroen H. wrote:

> This doesn’t address my original problem though, right? I mean in the  
> end you still call update\_attribute which generates the full update  
> SQL, not just for one field. Or am I missing something here…

Yes, as I wrote, unless you do custom SQL, Rails always saves entire  
records.  
The code I gave was just a way to cut out unnecessary saves in certain  
circumstances.

I’m not sure whether entire records are saved to simplify the Rails  
code,  
or because of some tricky issues that would arise if attributes could  
be updated individually. At the least you’d probably have to add a  
dirty  
bit to each record, or to each attribute in each record.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [December 23, 2005, 2:34pm UTC](https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173/5 "2005-12-23T14:34:13Z")

</div>

I too am observing this. Can anyone throw some light on this behaviour?

---

<div class="post-metadata">

**Author:** ![jeroen](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bbe5ce/32.png) [@jeroen](https://www.ruby-forum.com/u/jeroen)\
**Post date:** [December 23, 2005, 2:55pm UTC](https://www.ruby-forum.com/t/update-attribute-send-updates-every-field-why/48173/6 "2005-12-23T14:55:22Z")

</div>

> From ActiveRecord::Base:

```
 def update_attribute(name, value)
    send(name.to_s + '=', value)
    save
  end
 def save
    raise ActiveRecord::ReadOnlyRecord if readonly?
    create_or_update
  end

```

The call to ‘create\_or\_update’ will trigger either object#create or  
object#update, each of which contains the SQL code that persists the  
attributes in the object. You could probably override  
update\_attribute, and have it call a new method  
‘create\_or\_update\_lone\_attribute’ instead of ‘save’. Doesnt look like  
that would break anything.

On the other hand, why? Unless you have some huge binary columns, the  
db will update n columns in a record just about as quick as a single  
column.

cheers  
Gerret
