12

I want to put my chargify conf inside the initializer, but I found the initializer won't execute in my rails c, is there a way to invoke my initializers so I can test in my console?

    Chargify.configure do |c|
      c.api_key   = "keykey"
      c.subdomain = "test-site"
    end
user1883793
  • 4,011
  • 11
  • 36
  • 65
  • 2
    every file in `initializers` directory shall be auto-executed by Rails. Which also have effect in console – Shiva May 06 '16 at 04:41

3 Answers3

22

The config/initializers will execute, but only once, on initial load. So if you're making changes to config/initializers while the console is running you won't see the results of those changes happening.

Your best option is to stop and restart rails c, or you can type the reload! command in the console.

Also, if you are using spring that will sometimes prevent changed initializers from reloading. in that case do spring stop before you restart the console.

SteveTurczyn
  • 36,057
  • 6
  • 41
  • 53
  • 2
    The reason I was dubious is that when I want to log staff in initializers files using "p 'loaded?' ", it's doesn't always show up, even when I exited and then open my Rails console. – user1883793 May 07 '16 at 03:41
  • 1
    oh wow.. I didn't know exiting out of the rails console didn't stop spring. Wasted so many hours because of this :( Thank you so much for this answer! – Sandip Subedi Apr 30 '21 at 18:43
10

Yes, every .rb file in config/initializers is run whenever you run the console, run a rake task, or run your tests. Additonally, the environment configuration (config/environments) is run before the initializers.

Anthony E
  • 11,072
  • 2
  • 24
  • 44
  • 1
    Is there anyway to prevent this behavior, so to only run initialization code upon starting the server, but not on starting the console etc.? – Reinier Sep 14 '16 at 08:25
  • @Reinier use an environment variable. – Anthony E Sep 14 '16 at 09:20
  • It's possible to use a reference to the `Rails::Server` object with `if defined?(::Rails::Server)` in one of your initializers. – L.Youl Apr 12 '21 at 16:32
5

Apparently not anymore unless you disable spring:

export DISABLE_SPRING=1

pguardiario
  • 53,827
  • 19
  • 119
  • 159