# Writing form hash data to a model

**URL:** <https://www.ruby-forum.com/t/writing-form-hash-data-to-a-model/130958>\
**Category:** Rails\
**Created:** [March 4, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/writing-form-hash-data-to-a-model/130958 "2008-03-04T20:08:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [March 4, 2008, 8:08pm UTC](https://www.ruby-forum.com/t/writing-form-hash-data-to-a-model/130958/1 "2008-03-04T20:08:35Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [March 4, 2008, 8:16pm UTC](https://www.ruby-forum.com/t/writing-form-hash-data-to-a-model/130958/2 "2008-03-04T20:16:11Z")

</div>

self[formfield] = value  
should work

but i don’t see any sense in doing that…

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [March 4, 2008, 8:44pm UTC](https://www.ruby-forum.com/t/writing-form-hash-data-to-a-model/130958/3 "2008-03-04T20:44:08Z")

</div>

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](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](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.

---

<div class="post-metadata">

**Author:** ![jack](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/cc9497/32.png) [@jack](https://www.ruby-forum.com/u/jack)\
**Post date:** [March 5, 2008, 3:42am UTC](https://www.ruby-forum.com/t/writing-form-hash-data-to-a-model/130958/4 "2008-03-05T03:42:26Z")

</div>

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
