The Mystery of the Uninitialized Constant

With error driven development, you see a lot of red and pink on your web site. The trick is to learn how to interpret what the message is trying to tell you.

Most of them are pretty clear. If the word “path” appears, you’re probably missing something (or have a typo) in the routes list in your routes.rb file. If the word “relation,” or “table” appears, it’s probably the database.

This one was less obvious to me (ignore the line highlighted in pinkish-red, and read the text at the top of the image):

error message: uninitialized constant
the mystery uninitialized constant

I checked that the class existed, and that its name had no typos. I checked that it was being referenced correctly in the methods that needed it.

I checked that the things that need it (and that it needs) had relations specifications (“has_many” and “belongs_to”) in the right files.

I was stumped. So I asked for help.

And the answer is:

Model names are singular. The error message shows that the server was looking for a plural model name.

Hmmmm, so where in the app is it being used in plural form?

The model was properly defined, and its file name was correct, so “unwinding the stack” to the methods that use the model showed that they were also properly defined with no mystery plurals.

The next step was to look at the places where relations were specified, and voila! There was a plural instead of singular in the Tagging class’s “belongs_to” relation.

Yay! It’s fixed, now.

Here’s what should appear, instead of that error message:

image of an adorable rodent
much cuter than an error message!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Skip to content