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 ;-))
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…
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.
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
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.