Hi all, I was wondering if there is provision of direct matching of params hash and models. What I mean is, is there a way by which params[:user][:email] automatically gets assigned to user.email? What I know off is that you have to do user.email=params[:user][:email] I''ll appreciate if anyone helps me with this regards gaurav --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
user.set_attributes(params[:user]) should work... 2006/10/26, gaurav bagga <gaurav.v.bagga-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hi all, > > I was wondering if there is provision of direct matching of params hash > and models. > What I mean is, is there a way by which params[:user][:email] > automatically gets assigned to > user.email? > > What I know off is that you have to do user.email=params[:user][:email] > > I''ll appreciate if anyone helps me with this > > regards > gaurav > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
gaurav bagga wrote:> What I mean is, is there a way by which params[:user][:email] > automatically > gets assigned to > user.email?Depends on what you want to do with the params hash. There are a few commonly used ActiveRecord::Base class methods that take an attributes hash as an argument, so you can do: user = User.create(params[:user]) user = User.new(params[:user]) User.update(user_id, params[:user]) (I think that''s all of them.) See the ActiveRecord::Base documentation at http://api.rubyonrails.com/classes/ActiveRecord/Base.html for more details on these methods. Outside of those, I don''t think there''s any way to assign the params to a model object "automatically", though. -- 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 -~----------~----~----~----~------~----~------~--~---
Peter Ertl wrote:> user.set_attributes(params[:user]) > > should work...You''re right. DERRR. Time to go get some more coffee. Except I don''t think there''s a "set_attributes" method. I believe you mean one of the following two methods: user.update_attributes(params[:user]) user.attributes = params[:user] -- 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 -~----------~----~----~----~------~----~------~--~---