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.

thanks
reg

Reg Phipps 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.

thanks
reg

If I understand the question right, you want to add to the
params{:transfer] hash so you can use/modify it later, possibly during
a create.

One possibility is that if you are not using it right away is to just
store it in your session:

session[:transfer] = params[:transfer]
session[:transfer] [new_key]= new_val # then add vals to it as
required

then later on within the session…

SomeModel.create(session[:transfer])

hope this helps

ilan

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

Philip H. 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

Reg Phipps wrote:

Philip H. 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 ?

reg

Reg Phipps wrote:

Reg Phipps wrote:

Philip H. 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 ?

reg

Got it ! I used

  foo = {:status => 'pending', :initiation_method => 'user'}
  params[:transfer].update(foo)

  ...
  ...
  .create(params[:transfer])

The table is now created with the other columns

reg

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.