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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2008-Mar-27 10:30 UTC
Re: Active record - Help Updating single attributes
On 27 Mar 2008, at 10:06, Adam Jones 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> 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 > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi, Maybe the right questions 40 columns, code smell? Adam Jones 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-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 27 Mar 2008, at 10:06, Adam Jones 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 > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 28, 4:53 am, AndyV <a...-HmMyXyqgL2CVc3sceRu5cw@public.gmane.org> 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 Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> > 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!> > On 27 Mar 2008, at 10:06, Adam Jones 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 > > > > 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--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---