find() and then save() with select()

Does anybody know how to make the following code work:

invoice = Invoice.select('id, date').find(1) invoice.conf_date = Date.today invoice.save

As it's mentioned in guides "If the select method is used, all the returning objects will be read only" the code below throws an ActiveRecord::MissingAttributeError exception.

Any help will be highly appreciated!

There is no need for the select in the find, just do invoice = Invoice.find(1) invoice.conf_date = Date.today invoice.save

Colin

Unfortunately, i have to use select() since the table consists of many BLOB columns which i prefer not to fetch.

But why are fetching at all? If all you're doing is changing the date (although I see you select "date" [1] and update "conf_date"), why not use .update? http://apidock.com/rails/ActiveRecord/Base/update/class

[1] *Very* bad name for a column...

It seems "update_attribute" could do this.

(that is updating one field, without fetching the entire record with the large blobs).

1.9.3p0 :005 > o = Order.select('uuid, name').first   Order Load (0.8ms) SELECT uuid, name FROM "orders" ORDER BY orders.created_at LIMIT 1 => #<Order uuid: "6529fb26-6b6f-402c-a9af-cba0d7f4f288", name: "..."> 1.9.3p0 :006 > o.update_attribute :name, 'test'    (0.3ms) BEGIN    (68.6ms) UPDATE "orders" SET "name" = 'test', "updated_at" = '2012-03-04 14:53:02.743113' WHERE "orders"."uuid" = '6529fb26-6b6f-402c-a9af-cba0d7f4f288'    (30.1ms) COMMIT => true

HTH,

Peter

PS and if your table is arranged in such a way to prevent you from populating records, maybe a bit of remodelling those BLOBs out to associations would be in order?

1. update_attribute makes no valition 2. Column names are dummy 3. Model.update(id, attrs) is the way worth trying.

I just thought there is more convenient way to do that.

PS and if your table is arranged in such a way to prevent you from populating records, maybe a bit of remodelling those BLOBs out to associations would be in order?

That's not an option.

* 1. update_attribute makes no validation

But update_attributes does use validation. You can use update_attributes with only a single attribute.

Nope, i can't. It gets not saved.

Nope, i can't. It gets not saved.

Well you are doing it wrong then.

Colin