# Active record - Help Updating single attributes

**URL:** <https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061>\
**Category:** Rails\
**Created:** [March 27, 2008, 11:07am UTC](https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061 "2008-03-27T11:07:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sideshow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sideshow](https://www.ruby-forum.com/u/sideshow)\
**Post date:** [March 27, 2008, 11:07am UTC](https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061/1 "2008-03-27T11:07:24Z")

</div>

Hi ,

Can someone guide me as to how to update a single attribute/column  
rather than the whole row in rails 2.0  
My table has about 40 columns in it and i have a page that only wants  
to pull and update about 5 columns not the entire 40

if i do:

@listing = Listing.find(params[:listing\_id])  
@listing.update\_attribute(:description, “asdasd”)  
@listing.save

or

@listing = Listing.find(params[:listing\_id])  
@listing.udescription = “asdasd”)  
@listing.save

it selects all columns in the listing table and submits them all back  
upon commit which is pretty uneconomical ie  
" UPDATE `listings` SET `classification_id` = 102, `minimum_price` =  
NULL, `title` = … etc

cheers Adam

---

<div class="post-metadata">

**Author:** ![sideshow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sideshow](https://www.ruby-forum.com/u/sideshow)\
**Post date:** [March 27, 2008, 11:31am UTC](https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061/2 "2008-03-27T11:31:12Z")

</div>

On 27 Mar 2008, at 10:06, Adam J. wrote:

> Hi ,
> 
> Can someone guide me as to how to update a single attribute/column  
> rather than the whole row in rails 2.0  
> My table has about 40 columns in it and i have a page that only wants  
> to pull and update about 5 columns not the entire 40

That is how AR works currently. You could limit the damage by use  
a :select (ie Listing.find(params[:listing\_id], :select =\> ‘id, …’))

Fred

---

<div class="post-metadata">

**Author:** ![sideshow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sideshow](https://www.ruby-forum.com/u/sideshow)\
**Post date:** [March 27, 2008, 12:29pm UTC](https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061/3 "2008-03-27T12:29:44Z")

</div>

Hi,  
Maybe the right questions 40 columns, code smell?

Adam J. wrote:

> Hi ,
> 
> Can someone guide me as to how to update a single attribute/column  
> rather than the whole row in rails 2.0  
> My table has about 40 columns in it and i have a page that only wants  
> to pull and update about 5 columns not the entire 40
> 
> if i do:
> 
> @listing = Listing.find(params[:listing\_id])  
> @listing.update\_attribute(:description, “asdasd”)  
> @listing.save
> 
> or
> 
> @listing = Listing.find(params[:listing\_id])  
> @listing.udescription = “asdasd”)  
> @listing.save
> 
> it selects all columns in the listing table and submits them all back  
> upon commit which is pretty uneconomical ie  
> " UPDATE `listings` SET `classification_id` = 102, `minimum_price` =  
> NULL, `title` = … etc
> 
> cheers Adam

---

<div class="post-metadata">

**Author:** ![sideshow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sideshow](https://www.ruby-forum.com/u/sideshow)\
**Post date:** [March 27, 2008, 2:41pm UTC](https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061/4 "2008-03-27T14:41:56Z")

</div>

Definitely smells fishy from a data modeling point of view.

What may be more appropriate is to break down your Listing object into  
a main class and a one-to-many ListingProperty class. You can create  
accessor and mutator delegates for the properties in your main class,  
and access them by declaring property accessors in your main class.

It’s best to include only frequently accessed and queried columns in  
your main class.

---

<div class="post-metadata">

**Author:** ![sideshow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sideshow](https://www.ruby-forum.com/u/sideshow)\
**Post date:** [March 27, 2008, 4:54pm UTC](https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061/5 "2008-03-27T16:54:20Z")

</div>

This is pretty ugly but you could also use the class methods:

Listing.update\_all [“description = ? …”,  
‘asdasd’, …], :id=\>params[:listing\_id]  
=\> UPDATE listings SET description = ‘asdasd’, … WHERE  
`listings`.`id` = 2 (assuming params[:listing\_id] = 2)

On Mar 27, 6:30 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![sideshow](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/3e96dc/32.png) [@sideshow](https://www.ruby-forum.com/u/sideshow)\
**Post date:** [March 28, 2008, 12:17am UTC](https://www.ruby-forum.com/t/active-record-help-updating-single-attributes/133061/6 "2008-03-28T00:17:25Z")

</div>

On Mar 28, 4:53 am, AndyV [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This is pretty ugly but you could also use the class methods:
> 
> Listing.update\_all [“description = ? …”,  
> ‘asdasd’, …], :id=\>params[:listing\_id]  
> =\> UPDATE listings SET description = ‘asdasd’, … WHERE  
> `listings`.`id` = 2 (assuming params[:listing\_id] = 2)
> 
> On Mar 27, 6:30 am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> wrote:

Stink, i would have thought an AR update params would have just  
updated the params set before the update wether i have 6 or 40  
columns.  
I think this is how datamapper handles it.  
it makes it quite an intense update query if you have for example a  
full text column with 1000 words in it.  
Thanks for your replies!
