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? -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 3/1/07, Clem Rock <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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?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 --> Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Clem, Clem Rock 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Thu, Mar 01, 2007 at 01:48:32PM -0600, Bill Walton wrote:> > Hi Clem, > > Clem Rock 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.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> > 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].deleteThis 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Greg, Gregory Seidman 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?>> >> 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 > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mar 1, 2007, at 11:10 AM, Clem Rock wrote:> > 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? >params[''spaces.title''] = params.delete(:title) Cheers- -- Ezra Zygmuntowicz -- Lead Rails Evangelist -- ez-NLltGlunAUd/unjJdyJNww@public.gmane.org -- Engine Yard, Serious Rails Hosting -- (866) 518-YARD (9273) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sweet - Thanks! Always simpler than I think. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Clem Rock wrote:> Sweet - Thanks! > > Always simpler than I think.Do you know how I can add a key value -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---