I''m trying to take form data and put it into a model variable without using ActiveRecord''s features (save, create, update_attributes, etc.) that would write the record out to a database. So I''ve written a method like this in my Customer model: def modify(cust_info) cust_info.each do |formfield, value| formfield = value end end And Ive got a form where some of the form fields are all set up such that name="customer[the_field_name]" I''ve got a controller that has a customer object defined and I just want to modify certain fields based on what was submitted by use of the form: customer.modify(params[:customer]) But the modify method I defined (see above) doesn''t appear to work. How can I go through the hash and have each key in it be used to identify the appropriate field in the model''s object to assign the value? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
self[formfield] = value should work but i don''t see any sense in doing that... -- 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 -~----------~----~----~----~------~----~------~--~---
Jack wrote:> I''m trying to take form data and put it into a model variable without > using ActiveRecord''s features (save, create, update_attributes, etc.) > that would write the record out to a database.Whenever you want a variation on an ActiveRecord method, it''s always useful to check the implementation of the nearest candidate. You are trying to re-implement #update_attributes without the save. It seems reasonable to look at the documentation first: http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001436 Here you will find the source is: def update_attributes(attributes) self.attributes = attributes save end Following to the definition of #attributes http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M001447 you''ll see that this form will also stop you from updating protected attributes which is a bonus. -- 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 -~----------~----~----~----~------~----~------~--~---
why exactly do you want to avoid using the standard methods? You''re losing a lot of functionality that you''d otherwise receive for free. how does your method not work? after you call customer.modify..., do the corresponding attributes not have the new values set? or are you trying to figure out why they''re not saved to the database? Mike On Tue, Mar 4, 2008 at 2:08 PM, Jack <jlk4p-4Ng6DfrEGID2fBVCVOL8/A@public.gmane.org> wrote:> > I''m trying to take form data and put it into a model variable without > using ActiveRecord''s features (save, create, update_attributes, etc.) > that would write the record out to a database. > > So I''ve written a method like this in my Customer model: > > def modify(cust_info) > cust_info.each do |formfield, value| > formfield = value > end > end > > And Ive got a form where some of the form fields are all set up such > that name="customer[the_field_name]" > > I''ve got a controller that has a customer object defined and I just > want to modify certain fields based on what was submitted by use of > the form: > > customer.modify(params[:customer]) > > But the modify method I defined (see above) doesn''t appear to work. > How can I go through the hash and have each key in it be used to > identify the appropriate field in the model''s object to assign the > value? > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---