Forum: Ruby on Rails Swapping key names 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.
Clem R. (Guest)
on 2007-03-01 21:10
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?
Luke I. (Guest)
on 2007-03-01 21:49
(Received via mailing list)
On 3/1/07, Clem R. <removed_email_address@domain.invalid> 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

--
Bill W. (Guest)
on 2007-03-01 21:54
(Received via mailing list)
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
Gregory S. (Guest)
on 2007-03-01 22:50
(Received via mailing list)
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
Bill W. (Guest)
on 2007-03-01 23:34
(Received via mailing list)
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
Ezra Z. (Guest)
on 2007-03-02 02:41
(Received via mailing list)
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
-- removed_email_address@domain.invalid
-- Engine Y., Serious Rails Hosting
-- (866) 518-YARD (9273)
Clem R. (Guest)
on 2007-03-02 19:21
Sweet - Thanks!

Always simpler than I think.
Reg (Guest)
on 2007-03-16 17:53
Clem R. wrote:
> Sweet - Thanks!
>
> Always simpler than I think.
Do you know how I can add a key value
This topic is locked and can not be replied to.