the other dell

There can be only one

I recently added a gem to a little Rails project I’m building. The gem provided some classes and I followed a tutorial on how to use some of them. Early in the tutorial refreshed my browser and saw a Rails default error message page proclaiming “NameError uninitialized constant class”. You don’t expect errors when you follow tutorials (especially from RailsCasts, so I was thrown and knew it was something I’d done. I’m picking up Ruby and Rails now, so every error is both a real challenge and a learning experience.

Googling the error message brought up many related pages - a mix of blog posts and StackOverflow discussions. None of them quite fit my case or had solutions that suited more complicated apps than my tutorial code. One post by Akshey Mohite was of just this form, but ended up pointing me in the right direction anyway. Akshey’s opening remarks indicate that you “[can’t] call the function of the class which is not loaded with configuration”, which I interpreted to mean that all relevant code must be loaded in order to make use of any of it - simple enough. I had been follwoing the tutorial step by step and the author didn’t show any configuration that I hadn’t also done, so what was wrong? (Actually, they did, but I’ll explain that at the end.)

After sleeping on it, I found the solution. My general workflow involves running rails server and rails console side by side, in separate terminal windows. However, when rattling through quick code changes I often just refresh my browser window. On a whim I tried killing the server completely and quitting the console session, and then re-running the server. This solved the problem!

Which leads to the thought: there is only one instance of the application, even if server and console are both running. I had added the gem, run bundle and added code correctly, but because I hadn’t refreshed both the server session AND the console session (using the reload! console function), the server didn’t get all the newly updated code.

Im my Rails learning so far, I hadn’t seen this situation. I’d been merrily playing with models and data in console and seeing the changes in the browser; I’d been changing views and seeing the changes in the browser without reload!ing console. It hadn’t occurred to me the core of the app was the same in both environments because the views are hot-swappable and the models ultimately operate on a separate database instance (or SQLite file).

A word on differing configurations: The RailsCast tutorials all use a similar format application - it has a layout and colourscheme unique to the tutorials. I like to “code-along”, regularly pausing the video to try out the code shown, in short chunks, and then resuming. I don’t have a neat, themed app to run my code in, I just use Rails’ default stylings and scaffolding. I mention this because it highlights how I DO have a different rails configuration from the tutorials. It’s probably not a big difference, but I simply don’t know what is in the tutorial application that isn’t in a bare-bones Rails application.