[PhillyOnRails] ActiveRecord: updating a single attribute
Chris Cera
chris at cera.us
Thu Jan 24 10:33:58 MST 2008
Hi everyone,
Is there a way to update just one attribute of a record without
causing all the attributes to be re-set? When I use the
update_attribute() or increment(), since they call save() it
causes every attribute to get re-set. I verified this in the
mysql 'General Query Log'. Example:
item.update_attribute(:normal_views, item.normal_views + 1)
I feel that there must be a more elegant solution than this:
sql = 'UPDATE documents SET normal_views = normal_views + 1 WHERE id = ' + item.id.to_s
ActiveRecord::Base.connection.update(sql)
Any suggestions are greatly appreciated. Thank you, -Chris
ref: http://api.rubyonrails.org/classes/ActiveRecord/Base.html
--
Christopher D. Cera
CTO, Hovitate LLC
http://vuzit.com
P: 215.435.2289
F: 267.295.7369
More information about the talk
mailing list