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!

On 4 March 2012 09:09, arturturik [email protected] wrote:

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.

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.

On 4 March 2012 14:38, arturturik [email protected] wrote:

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…

On 4 March 2012 14:53, Michael P. [email protected] wrote:

On 4 March 2012 14:38, arturturik [email protected] wrote:

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?

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?

On Sun, Mar 4, 2012 at 3:38 PM, arturturik [email protected] wrote:

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.

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

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

  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.

    1. update_attribute makes no validation

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.

On 03/04/2012 08:10 AM, arturturik wrote:

    1. update_attribute makes no validation

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

On 4 March 2012 16:34, arturturik [email protected] wrote:

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

Well you are doing it wrong then.

Colin

To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.


gplus.to/clanlaw

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