Grant Ammons on 24 Jan 2008 08:48:22 -0800

[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PhillyOnRails] ActiveRecord: updating a single attribute

I was just thinking about this.  Why does the entire model's attributes get saved whilst calling update_attribute?  It has a feeling of inefficiency.

If you're looking for (semi) pure speed, go with just a raw update.

ActiveRecord::Base.connection.update("update users set last_seen_at = '#{}' where id = #{}")


On Jan 24, 2008 12:33 PM, Chris Cera <> wrote:
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 = ' +

Any suggestions are greatly appreciated.  Thank you, -Chris


Christopher D. Cera
CTO, Hovitate LLC
P: 215.435.2289
F: 267.295.7369
To unsubscribe or change your settings, visit:

To unsubscribe or change your settings, visit: