Swapping key names in a hash

Hello,

I'm trying to change the name of a hash key.   I want to take a

params[:value] and modify some of it’s key names to work better in model
sql statements.

for instance I have a param hash key named “title” (params[‘title’]) and
I want to change that key name to ‘spaces.title’ so it will work better
w/in a conditional statement in joined table sql call.

Any ideas?

On 3/1/07, Clem R. [email protected] wrote:

w/in a conditional statement in joined table sql call.

Any ideas?

params[‘spaces.title’] = params[‘title’]
?

Am I missing something?
If you want to make sure the other one is gone you can always do
params[‘title’] = nil

Hi Clem,

Clem R. wrote:

I’m trying to change the name of a hash key. I want to take a
params[:value] and modify some of it’s key names to work
better in model sql statements.

I don’t think you can change the name of an existing key. It’s easy to
create a new hash element with the new name and then delete the old one
though.

for instance I have a param hash key named “title” (params[‘title’]) and
I want to change that key name to ‘spaces.title’ so it will work better
w/in a conditional statement in joined table sql call.

I’m not 100% sure that Rails will let this work in the controller since
the
params hash is constructed by the browser and read in the controller.
Having said that, and assuming the key names you want to use are valid
for
Ruby hashes…

params[:spaces.title] = params[:title]
params[:title].delete

If that doesn’t work then you’ll probably have to construct a new hash,
copy
the params hash into it, and then modify it to suit your needs. There’s
probably a ‘slicker’ way to do it too. Maybe someone else will chime in
with one.

Best regards,
Bill

Hi Greg,

Gregory S. wrote:

It is possible to change an existing key; that’s why
Hash#rehash exists. That said, I just tried to actually
accomplish it with string keys and couldn’t manage it.
It does work with arrays, however:

Interesting. I’d tried it before, but just with a regular hash, not a
hash
of arrays. Like you, I couldn’t get it to work on the former. I’m
trying
to envision when I’d use an array as a key. What made you think to try
that?

This has syntax problems. Try this:

params[:“spaces.title”] = params.delete(:title)

I had a feeling the dot notation might cause a problem, but not the time
to
work through it. Thanks for the follow-through.

Best regards,
Bill

On Thu, Mar 01, 2007 at 01:48:32PM -0600, Bill W. wrote:

create a new hash element with the new name and then delete the old one
though.

It is possible to change an existing key; that’s why Hash#rehash exists.
That said, I just tried to actually accomplish it with string keys and
couldn’t manage it. It does work with arrays, however:

irb> x = { [1,2] => 3, [4,5] => 6 }
=> {[1, 2]=>3, [4, 5]=>6}
irb> x.keys
=> [[1, 2], [4, 5]]
irb> x.keys.first << 99
=> [1, 2, 99]
irb> x.keys
=> [[1, 2, 99], [4, 5]]
irb> x[[1,2]]
=> nil
irb> x[[1,2,99]]
=> nil
irb> x.rehash
=> {[4, 5]=>6, [1, 2, 99]=>3}
irb> x[[1,2,99]]
=> 3

params[:title].delete
This has syntax problems. Try this:

params[:“spaces.title”] = params.delete(:title)

If that doesn’t work then you’ll probably have to construct a new hash, copy
the params hash into it, and then modify it to suit your needs. There’s
probably a ‘slicker’ way to do it too. Maybe someone else will chime in
with one.

Best regards,
Bill
–Greg

On Mar 1, 2007, at 11:10 AM, Clem R. wrote:

I want to change that key name to ‘spaces.title’ so it will work
better
w/in a conditional statement in joined table sql call.

Any ideas?

params[‘spaces.title’] = params.delete(:title)

Cheers-

– Ezra Z.
– Lead Rails Evangelist
[email protected]
– Engine Y., Serious Rails Hosting
– (866) 518-YARD (9273)

Sweet - Thanks!

Always simpler than I think.

Clem R. wrote:

Sweet - Thanks!

Always simpler than I think.
Do you know how I can add a key value