Active record - Help Updating single attributes

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

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

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

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.

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. [email protected]

On Mar 28, 4:53 am, AndyV [email protected] 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. [email protected]
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!