1

One thing that keeps me banging my head on the wall in Rails is its unclean way of saving single attributes back to the model. Or at least, my understanding of it.

From what i know, the closest method that is doing that is update_attribute (which is now deprecated?). However, it has a major drawback. It performs an update on all model fields.

If i'm mistaken, please state what is the best way to do this thing in a clean manner. If i'm correct, i seriously don't understand, why there is not clean method that does this on single attributes?

Spyros
  • 46,820
  • 25
  • 86
  • 129
  • By "clean" do you mean that you want Rails to issue a "UPDATE X WHERE Y" for a single field? And [does this help](http://stackoverflow.com/questions/840199/is-the-rails-update-attributes-method-the-best-choice-for-doing-an-update-of-a-mo/840323#840323) (not sure what SQL it would generate.. check it out in the console) – Zabba Apr 25 '11 at 05:18
  • exactly that, no messing with the other attributes. – Spyros Apr 25 '11 at 05:19

1 Answers1

2

I just tested this out:

Code:

Order.update(1, :description => 'fff')

SQL executed:

UPDATE `orders` SET `updated_at` = '2011-04-25 05:23:29', `description` = 'fff' WHERE `orders`.`id` = 1

So yes, Rails update does almost what you need. (except that it also updates the updated_at)

Tip: In IRB, you can execute ActiveRecord::Base.logger = Logger.new(STDOUT) to see the log output of Rails (including SQL statments).

Zabba
  • 64,285
  • 47
  • 179
  • 207