ActiveRecord dumb on capitalized acronyms?

Hi,

I just ran into an unitialized contstant error for ‘Mp3’ with rails.
When I generated my model from the commandline, I used something like…
‘ruby script/generate model MP3’ and it created mp3.rb with an AR class
called ‘MP3’, so I figured it was ok, but now this doesn’t seem to work.

Going in and changing all references of the ‘MP3’ class to ‘Mp3’ seemed
to fix it, but it doesn’t look as nice since it’s supposed to be an
acronym. Is there any way I can make it respect those capitalization
rules? I haven’t gotten this far yet, but will I most likely run into
the same problem with a ‘CD’ class?

I am using InstantRails (thanks Curt!) updated to 1.0 RC5 gems. The
actual line that seems to have caused the problem was:

track.mp3 = MP3.new( :filename => ‘blah’ )

In the above, Track has_one :mp3 and MP3 belongs_to :track

Just for fun I tried ‘track.m_p3’ too and it didn’t work. Tried to
google for a solution since I’m sure I’m not the first, but I came up
empty handed.

Thanks in advance!

-Pawel

I dont think this is an AR issue, but just a general Ruby one. In
general all cap items are considered to be constants. Did you get an
error message? If so could you post it to confirm or deny this?

-Nick

On Dec 11, 2005, at 9:15 AM, Nick S. wrote:

I dont think this is an AR issue, but just a general Ruby one. In
general all cap items are considered to be constants. Did you get an
error message? If so could you post it to confirm or deny this?

-Nick

No, it’s a Rails issue. The issue is going from MP3 to mp3 is easy,
but going the other way is much less robust.

Consider a more enlightening example, say you’ve got a class RSSFeed

‘RSSFeed’.tableize
=> “rss_feeds”

That’s very easy, a simple rule saying if there are multiple capital
letters in a row, it’s a possible acronym, separate at the last one.

The problem is going the other way:

‘rss_feeds’.classify
=> “RssFeed”

Woops, there’s no reasonable way to know that ‘rss’ should be an acronym
rather than the word Rss.

You could likely hack in some special magic to make it work for a given
case, but it’s really easiest to just accept stuff like RssFeed and Mp3.

It’s not perfect, but it’s also a difficult problem to solve with a
practical solution (ie not keeping a giant list of all possible
acronyms).


Scott B.
Lunchbox Software
http://lunchboxsoftware.com
http://lunchroom.lunchboxsoftware.com
http://rubyi.st

Thanks for the replies Nick and Scott,

No, it’s a Rails issue. The issue is going from MP3 to mp3 is easy,
but going the other way is much less robust.

Yeah, I figured ruby would be OK with it since we have a YAML module and
probably many other examples.

Woops, there’s no reasonable way to know that ‘rss’ should be an acronym
rather than the word Rss.

You could likely hack in some special magic to make it work for a given
case, but it’s really easiest to just accept stuff like RssFeed and Mp3.

It’s not perfect, but it’s also a difficult problem to solve with a
practical solution (ie not keeping a giant list of all possible
acronyms).

That makes sense now that you explain it that way. Though I would think
it would be possible to generate some sort of a lookup table at model
generation time or something - especially since the generate script
honors the capitalization given on the command line. I’d think that
(knowing it won’t work correctly up front) there would at least be some
automagical capitalization (or just a round trip conversion of MP3 class
-> mp3 attribute -> Mp3 class) fix from MP3 -> Mp3 in the same graceful
way that reserved words are handled.

Do either of those sound like a reasonable solution to anyone?

-Pawel