Chris Cera on 24 Jan 2008 09:37:30 -0800


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

Re: [PhillyOnRails] ActiveRecord: updating a single attribute


Hi Walter,

It seems inefficient at a first glance, but Evan's point about
validation makes sense.

In this particular instance it had to do with how we implemented
mutual exclusion.  Since the status field was getting reset (to
the same value), it caused a race condition.

Thanks for the helpful comments, -Chris


* Walter Lee Davis <waltd@wdstudio.com> [080124 12:44]:
> Is there a reason for you that this is important? Is there any  
> difference functionally between
> 
> update documents set field1 = [current value], ... fieldN = [current  
> value], normal_views = X where id = 123
> 
> and
> 
> update documents set normal_views - X where id = 123
> 
> ??
> 
> Other than brevity of SQL logs, anyway?
> 
> Walter
> 
> On Jan 24, 2008, at 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 = ' + 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
> >_______________________________________________
> >To unsubscribe or change your settings, visit:
> >http://lists.phillyonrails.org/mailman/listinfo/talk

-- 
Christopher D. Cera
CTO, Hovitate LLC
http://vuzit.com
P: 215.435.2289
F: 267.295.7369
_______________________________________________
To unsubscribe or change your settings, visit:
http://lists.phillyonrails.org/mailman/listinfo/talk