Weird symbol gotcha in ActiveRecord
Perhaps you already knew this, but here’s my (first) hint for the day: don’t store Symbols (you know the ones — :my_symbol etc) in your database. It would
seem that ActiveRecord doesn’t to a to_s on them. Instead it seems to do Mystery Operation X. This seems to basically convert:
:my_symbol
to something like
---- :my_symbol
Which could be fine. Possibly even useful — hey, I might actually want Symbols stored in the DB like that (assuming I never want to store strings like that…) Unfortunately it doesn’t seem to be paired with
Mystery Operation Un-X. In that when it comes back out of the database, nothing is done; you just get a String (with garbage characters chucked in), not a Symbol, and it’s totally useless. My
advice: don’t store symbols in the DB.
This could be due to my use of the Composed Of Conversion plugin (which I’m just about to write an entry about), but if so, since it happens one way but not the other, it’s an/yet-another example of deep-embedded Rails code cruftyness.
(Incidentally, if someone can explain this, that would be great. That whole explaining process would probably be made easier if I had comments on this blog… Oh well.)