Can't access model attributes via symbol

If I do a:

In controller:
@x = User.find(:all)

In view:
<% for rec in @x -%>

<%= rec[:username] %>

<% 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

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…

Philip H. 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?

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 B. 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. :wink:

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

Vishal wrote:

If I do a:

In controller:
@x = User.find(:all)

In view:
<% for rec in @x -%>

<%= rec[:username] %>

<% 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

You can definitely access attributes with symbols, I just checked it.

p = Person.find(:first)
p[:first_name] # => “Jonathan”

-Jonathan.

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. :wink:

-Rob

Rob B. http://agileconsultingllc.com
[email protected]