Wes Gamble
2007-Jan-20 00:53 UTC
Best way to update attributes on AR objects without saving?
All, In some cases, one may not want to save attributes retrieved from a form to the database right away. Would the best way to bulk update your AR object from the params hash be to do: @ar_obj.attributes.merge!(params[:appropriate_key]) ? This seems to be the the best approach to me. For some reason, I was under the impression that update_attributes didn''t update the record in the database and so now I''m trying to find the appropriate replacement. Thanks, Wes -- 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 -~----------~----~----~----~------~----~------~--~---
s.ross
2007-Jan-20 01:23 UTC
Re: Best way to update attributes on AR objects without saving?
How about: @ar_obj = ArObj.new(params[:id]) if @ar_obj.valid? # whatever code you need here for good objects @ar_obj.save else # whatever code you have for erroneous information end AR is smart enough to figure out whether a record already exists by primary key, so save will INSERT if it''s a new record or UPDATE if it''s an existing one. Updating attributes skips straight to the database before giving you a look. That''s why creating from the params has is so powerful. So what if you don''t have a params hash? Make one: @ar_obj = ArObj.new({:id => 3, :title = ''yippie skippy''}) Yes? athem wrote:> > > All, > > In some cases, one may not want to save attributes retrieved from a form > to the database right away. Would the best way to bulk update your AR > object from the params hash be to do: > > @ar_obj.attributes.merge!(params[:appropriate_key]) > > ? This seems to be the the best approach to me. > > For some reason, I was under the impression that update_attributes > didn''t update the record in the database and so now I''m trying to find > the appropriate replacement. > > Thanks, > Wes > > -- > Posted via http://www.ruby-forum.com/. > > > > >-- View this message in context: http://www.nabble.com/-Rails--Best-way-to-update-attributes-on-AR-objects-without-saving--tf3043495.html#a8460521 Sent from the RubyOnRails Users mailing list archive at Nabble.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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Jan-20 11:47 UTC
Re: Best way to update attributes on AR objects without saving?
Wes Gamble wrote:> All, > > In some cases, one may not want to save attributes retrieved from a form > to the database right away. Would the best way to bulk update your AR > object from the params hash be to do: > > @ar_obj.attributes.merge!(params[:appropriate_key])@ar_obj.attributes = params[:appropriate_key] is all you need. The only attributes set are those named by keys in the params hash. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2007-Jan-20 18:36 UTC
Re: Best way to update attributes on AR objects without savi
Mark Reginald James wrote:> @ar_obj.attributes = params[:appropriate_key] > > is all you need. The only attributes set are those named > by keys in the params hash.Shouldn''t you merge the keys, given the possibility that your object may have an unsaved attribute set already (that you want to keep) that is not represented in your params hash. Using = will blow away those attributes, but merge! will preserve them. Thanks, Wes -- 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 -~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2007-Jan-20 19:29 UTC
Re: Best way to update attributes on AR objects without savi
Wes Gamble wrote:> Mark Reginald James wrote: > >> @ar_obj.attributes = params[:appropriate_key] >> >> is all you need. The only attributes set are those named >> by keys in the params hash. > > Shouldn''t you merge the keys, given the possibility that your object may > have an unsaved attribute set already (that you want to keep) that is > not represented in your params hash. Using = will blow away those > attributes, but merge! will preserve them.No, the attributes= method does not assign a new attribute hash. It just assigns new values to the attributes (and attr_accessors) keyed in the RHS hash. -- We develop, watch us RoR, in numbers too big to ignore. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Pat Maddox
2007-Jan-20 19:46 UTC
Re: Best way to update attributes on AR objects without savi
On 1/20/07, Wes Gamble <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Mark Reginald James wrote: > > > @ar_obj.attributes = params[:appropriate_key] > > > > is all you need. The only attributes set are those named > > by keys in the params hash. > > Shouldn''t you merge the keys, given the possibility that your object may > have an unsaved attribute set already (that you want to keep) that is > not represented in your params hash. Using = will blow away those > attributes, but merge! will preserve them.Actually it doesn''t blow them away. That''s something that I forget every single time I use it. Also, because merge! is a method on Hash, using that will bypass things like attr_protected, which you don''t want. Pat --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Wes Gamble
2007-Jan-20 20:36 UTC
Re: Best way to update attributes on AR objects without savi
OK I''m convinced. I had tested using "=" instead of merge! and I had convinced myself that "=" would blow away keys. But I just retested it and it works as described. I must have made a mistake on the first test. Interestingly (well, maybe not...) enough, I was attempting to use merge! and I could not get it to work to copy a params hash into an attribute hash, even though I verified the existence of the same keys (of the same "type") in the two hashes, and I was able to copy the values across fine if I used an interator on the params hash. I recalled that the params hash is a HashWithIndifferentAccess and the attributes Hash is a regular Hash - maybe that has something to do with it. Weird. Wes -- 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 -~----------~----~----~----~------~----~------~--~---