# Direct matching of params hash to model

**URL:** <https://www.ruby-forum.com/t/direct-matching-of-params-hash-to-model/73711>\
**Category:** Rails\
**Created:** [October 26, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/direct-matching-of-params-hash-to-model/73711 "2006-10-26T16:20:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [October 26, 2006, 4:20pm UTC](https://www.ruby-forum.com/t/direct-matching-of-params-hash-to-model/73711/1 "2006-10-26T16:20:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [October 26, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/direct-matching-of-params-hash-to-model/73711/2 "2006-10-26T16:54:08Z")

</div>

user.set\_attributes(params[:user])

should work…

2006/10/26, gaurav bagga [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [October 26, 2006, 4:29pm UTC](https://www.ruby-forum.com/t/direct-matching-of-params-hash-to-model/73711/3 "2006-10-26T16:29:45Z")

</div>

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 [Peak Obsession](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.

---

<div class="post-metadata">

**Author:** ![gaurav\_bagga](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/a8b319/32.png) [@gaurav\_bagga](https://www.ruby-forum.com/u/gaurav_bagga)\
**Post date:** [October 26, 2006, 5:06pm UTC](https://www.ruby-forum.com/t/direct-matching-of-params-hash-to-model/73711/4 "2006-10-26T17:06:42Z")

</div>

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]
