Direct matching of params hash to model

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

user.set_attributes(params[:user])

should work…

2006/10/26, gaurav bagga [email protected]:

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.

Peter E. 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]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs