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开发者_如何学JAVA 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?
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).
精彩评论