Updating just one property of an ActiveRecord

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

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.

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:inupdate’

(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.

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

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. :slight_smile:

Thanks!!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs