Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add initialization step to Rails 3 boot process only in server mode

According to http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html, if I write a Rails 3 plugin and I want to hook into the initialization process, I write

class MyRailtie < Rails::Railtie
  initializer "my_railtie.configure_rails_initialization" do
    # some initialization behavior
  end
end

However, this initializer appears to be executed when you run, for instance, a Rails rake task, not just when you run rails s or similar. My question is, how do I prevent my code in this block from being run during Rails tasks, as opposed to full Rails server boot-ups? This seems to be a common problem with Rails 3 plugins.

like image 443
Trevor Burnham Avatar asked Aug 21 '10 23:08

Trevor Burnham


1 Answers

add this block to your initializer:

if defined?(Rails::Server)
  # do something
end

this should work with the current 3.0.6 rails version.

like image 141
jelveh Avatar answered Oct 07 '22 14:10

jelveh