Forum: Ruby on Rails Adding key value pairs in a hash

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Reg P. (Guest)
on 2007-03-16 18:31
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
Ilan B. (Guest)
on 2007-03-16 18:53
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
Philip H. (Guest)
on 2007-03-16 18:56
(Received via mailing list)
> 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 P. (Guest)
on 2007-03-16 19:34
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 P. (Guest)
on 2007-03-16 20:02
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 P. (Guest)
on 2007-03-16 21:07
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
Philip H. (Guest)
on 2007-03-16 21:41
(Received via mailing list)
>>>
>> 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.
This topic is locked and can not be replied to.