Flinn Mueller on 24 Jan 2008 10:49:59 -0800


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

Re: [PhillyOnRails] ActiveRecord: updating a single attribute

  • From: Flinn Mueller <theflinnster@gmail.com>
  • To: talk@phillyonrails.org
  • Subject: Re: [PhillyOnRails] ActiveRecord: updating a single attribute
  • Date: Thu, 24 Jan 2008 13:49:33 -0500
  • Cc:
  • Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:cc:message-id:from:to:in-reply-to:content-type:mime-version:subject:date:references:x-mailer; bh=+BlsjpbqhSfgizs5aBv/JIUHcoIb0ItKILbNdlwNOoA=; b=AkunSwdFVwlrTk3A1w2oXmNCe3HHuroQOnXZ/qV/4zh95y9N5D/3TRMc/lhEuLBRJUoIpdtZtuI466McN5Fdga7BkU0dFh90e+wenHgMKQMfmnp8SP5kcuehtirTIpIRE5EIno5zAOEJ5qETpZvr2+DDjR+ioPw8RLO4oCQPzrQ=
  • List-archive: <http://lists.phillyonrails.org/pipermail/talk>
  • Reply-to: talk@phillyonrails.org
  • Sender: talk-bounces@phillyonrails.org

On Jan 24, 2008, at 11:47 AM, Grant Ammons wrote:
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.

Updating each column seems like it needs a select to reexamine what needs to be updated.  Also you're talking about casting between current ruby values to possible new db values then comparing them with their values before casting then assembling what needs to be updated.  If there aren't any quantifiable reasons to do this I'd rather just worry about locking where I need it and worrying about more obvious performance issues.

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

ActiveRecord::Base.connection.update("update users set last_seen_at = '#{Time.now.to_s(:db)}' where id = #{logged_in_user.id}")


Doesn't this defeat the purpose of ActiveRecord?  Also does that give any speed advantage over the traditional method when not dealing with blobs?  I've seen this mentioned before and heard criticisms from some PHP guys but I'm not sure there is any quantifiable data showing a difference (not saying there isn't just saying I haven't seen it).  Chris, this seems like a question someone new to Rails might ask so if you're not yet married to ActiveRecord you might consider DataMapper ( http://datamapper.org ) which I believe offers this behavior.




-Grant

On Jan 24, 2008 12:33 PM, Chris Cera < chris@cera.us> 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

_______________________________________________
To unsubscribe or change your settings, visit:
http://lists.phillyonrails.org/mailman/listinfo/talk

_______________________________________________
To unsubscribe or change your settings, visit:
http://lists.phillyonrails.org/mailman/listinfo/talk