If I do a: In controller: @x = User.find(:all) In view: <% for rec in @x -%> <h1><%= rec[:username] %></h1> <% end -%> It doesn''t work. However, rec["username"] works. If I look with the breakpointer, of course all keys that ActiveRecord returns are strings. How can I access by symbol instead? (It is important so I can stay DRY ;-)) Thanks, Vishal -- 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 -~----------~----~----~----~------~----~------~--~---
> If I do a: > > In controller: > @x = User.find(:all) > > In view: > <% for rec in @x -%> > <h1><%= rec[:username] %></h1> > <% end -%> > > It doesn''t work. However, rec["username"] works. > If I look with the breakpointer, of course all keys > that ActiveRecord returns are strings. How can I access > by symbol instead? (It is important so I can stay DRY ;-))rec[:username.to_s] would do it... just convert your symbol to a string... --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom wrote:>> It doesn''t work. However, rec["username"] works. >> If I look with the breakpointer, of course all keys >> that ActiveRecord returns are strings. How can I access >> by symbol instead? (It is important so I can stay DRY ;-)) > > rec[:username.to_s] > > would do it... just convert your symbol to a string...Sure...but that seems like a hack. Is there a particular reason why ActiveRecord returns string based keys for queries and not symbols? Is there a way to fix this? -- 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 Nov 10, 2006, at 6:58 PM, Vishal wrote:> > Philip Hallstrom wrote: >>> It doesn''t work. However, rec["username"] works. >>> If I look with the breakpointer, of course all keys >>> that ActiveRecord returns are strings. How can I access >>> by symbol instead? (It is important so I can stay DRY ;-)) >> >> rec[:username.to_s] >> >> would do it... just convert your symbol to a string... > > Sure...but that seems like a hack. Is there a particular reason > why ActiveRecord returns string based keys for queries and > not symbols? Is there a way to fix this?Why not just: rec.username or if you really feel the need to use a symbol: rec.send(:username) but let the ActiveRecord attribute methods do their thing. ;-) -Rob Rob Biedenharn http://agileconsultingllc.com Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
I write some code that displays some stuff retrieved from a web service and stuffs it all into a ruby hash. The view that displays the hash uses symbols to access attributes. I originally wanted to reuse this same view when I pull some data I cached (in a DB) from the web service - this is my reason for wanting to use symbols. However it looks like I''ll have to create two separate views which essentially do the same thing. Oh well... Rob Biedenharn wrote:> On Nov 10, 2006, at 6:58 PM, Vishal wrote: > >> >> Sure...but that seems like a hack. Is there a particular reason >> why ActiveRecord returns string based keys for queries and >> not symbols? Is there a way to fix this? > > Why not just: > > rec.username > > or if you really feel the need to use a symbol: > > rec.send(:username) > > but let the ActiveRecord attribute methods do their thing. ;-) > > -Rob > > Rob Biedenharn http://agileconsultingllc.com > Rob-xa9cJyRlE0mWcWVYNo9pwxS2lgjeYSpx@public.gmane.org-- 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 -~----------~----~----~----~------~----~------~--~---
Vishal wrote:> If I do a: > > In controller: > @x = User.find(:all) > > In view: > <% for rec in @x -%> > <h1><%= rec[:username] %></h1> > <% end -%> > > It doesn''t work. However, rec["username"] works. > If I look with the breakpointer, of course all keys > that ActiveRecord returns are strings. How can I access > by symbol instead? (It is important so I can stay DRY ;-)) > > Thanks, > VishalWhen you say "it doesn''t work" what does it do? AR is supposed to be able to handle that fine and I have done it myself. It takes the same argument as read_attribute, which according to the docs, taks a symbol http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000918 -- 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 -~----------~----~----~----~------~----~------~--~---
You can definitely access attributes with symbols, I just checked it. p = Person.find(:first) p[:first_name] # => "Jonathan" -Jonathan. On 11/11/06, Alex Wayne <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Vishal wrote: > > If I do a: > > > > In controller: > > @x = User.find(:all) > > > > In view: > > <% for rec in @x -%> > > <h1><%= rec[:username] %></h1> > > <% end -%> > > > > It doesn''t work. However, rec["username"] works. > > If I look with the breakpointer, of course all keys > > that ActiveRecord returns are strings. How can I access > > by symbol instead? (It is important so I can stay DRY ;-)) > > > > Thanks, > > Vishal > > When you say "it doesn''t work" what does it do? > > AR is supposed to be able to handle that fine and I have done it myself. > It takes the same argument as read_attribute, which according to the > docs, taks a symbol > > http://api.rubyonrails.com/classes/ActiveRecord/Base.html#M000918 > > -- > 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 -~----------~----~----~----~------~----~------~--~---