Defaultblog

New Modules, Classes, Variables from Gem not loading in my Rails Application

ruby on rails ruby gems software development

So I just spent about 3 to 4 hours trying to debug this problem. Here's the setup

  • We have a Rails application that uses a Gem that we wrote
  • The Gem is pretty simple - a collection of classes that the Rails application can use (https://github.com/bitesite/spire-ruby)
  • Our top level gem file autoloads in files and some are within modules/namespaces

So my main task was adding a new class, within a module and no matter what I did, I would constantly get a 'constant not found' errors or something similar. I would restart my rails console, re-bundle - nothing would work.

At one point, if I manually called 'load' it would work, but not upon the initial load. I found out later that 'spring' was the issue. I'm not sure the specifics, but spring must cache the classes, modules, etc. it uses from gems.

So what's the fix? When adding new classes, modules, and requiring them in your top level my-gem.rb file, be sure to stop spring using

spring stop

so that when you restart your Rails server or console, it properly loads them. This is a super good article to learn the internals of how all this works: https://medium.com/@connorstack/understanding-ruby-load-require-gems-bundler-and-rails-autoloading-from-the-bottom-up-3b422902ca0

Caseyli
Casey Li
CEO & Founder, BiteSite