Search code examples
ruby-on-railsrubyrakeinitializer

Ruby on Rails: Error when running a rake task from initializer file


I have the file config/initializers/custom.rb

In the file, there is only one line:

`rake thinking_sphinx:start`

I thought this was supposed to just execute the line like when typing it from a command line. With this line of code, when I run "ruby script/server", the server freezes and outputs no error messages. Am I missing something?

Thanks!


Solution

  • Initializers load when your application loads. Rake tasks generally load your application. If you call a Rake task from an initializer, you're going to throw your app for a loop.

    If you're worried about forgetting to start Sphinx in development, just give yourself a little warning:

    # config/initializers/custom.rb
    begin
      ThinkingSphinx::Search.search "test" # test search
    rescue ThinkingSphinx::ConnectionError
      puts "** Oops! ThinkingSphinx is off! **"
    end