Writing form hash data to a model

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?

self[formfield] = value
should work

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

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.

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