Adding key value pairs in a hash

I have a controller which is passed params[:transfer] from a form. I
want to add some key vale pairs so the .create(params[:transfer] used
later has the other data base columns not populated in the form.

Foo.create( {:foo => 'bar', :one => 2}.merge(params[:transfer]) )

This way you can set *all* the defaults and anything specified in params[:transfer] will over write them...

-philip

Reg Phipps wrote:

Philip Hallstrom wrote:

I have a controller which is passed params[:transfer] from a form. I
want to add some key vale pairs so the .create(params[:transfer] used
later has the other data base columns not populated in the form.

Foo.create( {:foo => 'bar', :one => 2}.merge(params[:transfer]) )

This way you can set *all* the defaults and anything specified in
params[:transfer] will over write them...

-philip

Thanks, I'll try it out and let you know.

Another option may be to set the other fields (which have fixed values)
through the view by using MagicFieldNames
http://wiki.rubyonrails.org/rails/pages/MagicFieldNames

Does that make sense ?

reg

philip, sorry I don't quite understand. Is Foo the hash? with two key
value pairs where
key is :foo and value is 'bar'
key is :one and valie is '2'
and this is then merged with params[:transfer]

Don't I need to declare Foo ?

I used Foo to represent whatever your model is.