Mixup between symbols and strings in your code?


#1

I seem to have often had the problem that I end up writting code where
I get strings and symbols mixed up. Maybe this is because I like to do
pattern matches alot and end up with strings. Sometimes when you enter
a controller the paramter is a string, I forget exactly. I often end up
writting stuff like x = x.to_sym it seems like to be safe. I seem to
have had problems comparing a string to a symbol and while they where
the same identifier, they didn’t match because one was a string and the
other a symbol. This has caused me to have bugs that took a while to
figure out and had happened quite often. I am wondering if other folks
have had this problem as well ?


#2

Hi –

On Thu, 28 Dec 2006, surf wrote:

have had this problem as well ?
Yes, I have. Specifically, I sometimes stumble on cases where Rails
cares whether something is a symbol vs. string, and which it has to
be. At this point I’ve got most of it nailed, but there are one or
two that still get me sometimes. For example in:

has_many :messages, :through => :discussions

:discussions has to be a symbol. If it’s a string, you get the
potentially somewhat puzzling error message: “Could not find the
association “discussions” in model User”. I guess it’s supposed to be
a symbol in order to match the symbol-ness of the association names
themselves. For some reason I’ve had a hard time remembering this
one.

David


Q. What is THE Ruby book for Rails developers?
A. RUBY FOR RAILS by David A. Black (http://www.manning.com/black)
(See what readers are saying! http://www.rubypal.com/r4rrevs.pdf)
Q. Where can I get Ruby/Rails on-site training, consulting, coaching?
A. Ruby Power and Light, LLC (http://www.rubypal.com)