# Updating just one property of an ActiveRecord

**URL:** <https://www.ruby-forum.com/t/updating-just-one-property-of-an-activerecord/73846>\
**Category:** Rails\
**Created:** [October 27, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/updating-just-one-property-of-an-activerecord/73846 "2006-10-27T15:32:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [October 27, 2006, 3:32pm UTC](https://www.ruby-forum.com/t/updating-just-one-property-of-an-activerecord/73846/1 "2006-10-27T15:32:20Z")

</div>

All I want to do is change one field of my object and update it in the  
database. I thought that this was the syntax:

```
obj.update_attribute(:status_id, 5)

```

but the SQL statement that’s generated it still attempting to specify  
values for every single field in the object. This causes me problems  
(one of the fields is a very long XML object) so I’m trying to avoid it.  
What’s the Ruby syntax that will generate just the single statement to  
update object\_table set status\_id=5 where id=xxx?

Thanks!

D

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [October 27, 2006, 4:05pm UTC](https://www.ruby-forum.com/t/updating-just-one-property-of-an-activerecord/73846/2 "2006-10-27T16:05:03Z")

</div>

> What’s the Ruby syntax that will generate just the single statement to  
> update object\_table set status\_id=5 where id=xxx?
> 
> Thanks!
> 
> D

Try

Model.update(xxx, {:status\_id =\> 5})

According to AWDwR that should run a single update on that field, and  
not all of them.

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [October 27, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/updating-just-one-property-of-an-activerecord/73846/3 "2006-10-27T16:20:14Z")

</div>

Bryan M wrote:

> Model.update(xxx, {:status\_id =\> 5})  
> According to AWDwR that should run a single update on that field, and  
> not all of them.

Thanks Bryan. Unfortunately its not working for me. Using this:

Letter.update(l.id, { :status\_id =\> Status.batchsent })

gets me this:

c:/newruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active\_record/connecti  
on\_adapters/abstract\_adapter.rb:120:in `log': OCIError: ORA-01704: string litera l too long: UPDATE LETTER_TABLE SET xml_data = '...', recipient_email_address = '...', date_created = '2006-10-13 15:33:22', xsl_id = 2.0, user_id_created = '...', comments = '...' ..., status_id = 8, ltr_viewed = 0, is_deleted = 0, ready_for_review = 1 WHERE id = '...' (ActiveRecord::StatementInvalid) from c:/newruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/oracle_adapter.rb:271:in`update’

(edited for length. But that xml\_data=’…’ shouldn’t be there at all  
,that’s what I’m trying to prevent.)

I wonder if it’s because I’m using Oracle? Maybe my next move is to  
test it with MySql. That won’t help me, since I’m hitting a production  
database I can’t change, but at least it’ll tell me where the error  
lies.

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [October 27, 2006, 5:11pm UTC](https://www.ruby-forum.com/t/updating-just-one-property-of-an-activerecord/73846/4 "2006-10-27T17:11:50Z")

</div>

Duane M. wrote:

> update object\_table set status\_id=5 where id=xxx?

Duane,

I think Model.update\_all will do what you want. You can get the exact  
syntax from Rails doc.

Long  
www.edgesoft.ca

---

<div class="post-metadata">

**Author:** ![dmorin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/f0a364/32.png) [@dmorin](https://www.ruby-forum.com/u/dmorin)\
**Post date:** [October 27, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/updating-just-one-property-of-an-activerecord/73846/5 "2006-10-27T17:20:40Z")

</div>

Long wrote:

> Duane M. wrote:
> 
> > update object\_table set status\_id=5 where id=xxx?
> 
> Duane,
> 
> I think Model.update\_all will do what you want. You can get the exact  
> syntax from Rails doc.
> 
> Long  
> www.edgesoft.ca

Well I’ll be darned, that worked! Funny, that’s exactly the sort of  
thing I would have expected _not_ to work, what with the “update _all_”.  
It’s not terribly pretty:

Model.update\_all “status\_id=”+var.status.to\_s, “id=”+currentobj.id

but heck, I can probably clean that up. As long as it works I can move  
on. 🙂

Thanks!!
