Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Rails Console reload initializers? [duplicate]

I was using the Rails Console to test a few things by hand and found that one of my initializers (under config/initializers) had an error. I exited the console, made the change, and ran "rails c" again. Much to my surprise, the updated initializer was not executed.

Here is what I found:

-- If I start a new Bash session and enter "rails c" all the initializers get executed.

-- If I add / change an initializer and then re-run "rails c" within the same Bash session, the initializers are not executed.

This occurs under development which disables caching.

What the heck is going on? Thanks.

like image 925
Brendan Avatar asked Sep 27 '15 18:09

Brendan


1 Answers

The introduction of Spring was meant to make loading your rails app faster, by keeping a copy of the app in memory and only reloading what changes.

Unfortunately, sometimes your initializers can stay exactly the same, but they need to be re-run. An example is the "business_time" gem, which generates an initializer that loads a YAML file. You can change the YAML file and restart console, but Spring sees that your initializer hasn't changed, so it doesn't re-run it. Thus, the changes to your YAML file go unnoticed, This is a very difficult error to troubleshoot.

If you're having initializer problems, then exit out of console and run:

bundle exec spring stop

before going back into console. This will force a fresh reload of the initializers.

You can also disable Spring entirely by removing the gem from your Gemfile.

like image 157
Jaime Bellmyer Avatar answered Nov 14 '22 13:11

Jaime Bellmyer