Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Railtie initializers are not executed in rake tasks

#5878

Consider this railtie:


#config/environment.rb 
require File.expand_path('../application', __FILE__)

class Railtie < ::Rails::Railtie

    puts "0"

    initializer "translation.connection" do |app|

      puts "A"

      ActiveSupport.on_load(:active_record) do

        puts "B"

      end

    end

end

# Initialize the rails application
Initial::Application.initialize!

And this rake task

# lib/tasks/e.rake 
task :e => [:environment] do
end

When running from console everything works as expected:

> rails c
0
A
B

however running rake task leads to different result:

 > rake e
(in /home/rupert/initializer_bug)
0

Reported by Robert Pankowecki · October 28th, 2010 @ 03:51 PM

State: stale
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Jeff Kreeftmeijer
    Jeff Kreeftmeijer
    • Tag cleared.
    • Importance changed from to Low

    Automatic cleanup of spam.

    November 1st, 2010 @ 05:04 PM

  2. Matthew Ratzloff
    Matthew Ratzloff

    I'm having this issue, too. Anyone have a workaround?

    November 12th, 2010 @ 05:38 PM

  3. Robert Pankowecki
    Robert Pankowecki
    • Tag set to initializers, railties, rake

    The workaround is to execute the railtie initializers before in the rake task

    November 14th, 2010 @ 04:23 PM

  4. Robert Pankowecki
    Robert Pankowecki
    • Tag changed from initializers, railties, rake to initializers, rails3.0.1, railties, rake

    November 14th, 2010 @ 04:24 PM

  5. Robert Pankowecki
    Robert Pankowecki

    Here is my workaround:

    # Rails.root/Rakefile
    
    # Append
    task :environment do
      # https://rails.lighthouseapp.com/projects/8994/tickets/5878-railtie-initializers-are-not-executed-in-rake-tasks
      MyModule::Railtie.initializers.first.run(Rails.application)
    end
    

    I've got only one initializer in definied in my Railtie.

    I hope this will help unit the issue is resolved.

    November 18th, 2010 @ 11:33 AM

  6. Piotr Sarnacki
    Piotr Sarnacki

    It appears to work on edge, failing test didn't fail: https://github.com/drogus/rails/commit/a2c5de06fe47f85199d766de98d1...

    Please use workaround and wait for stable 3.1

    November 18th, 2010 @ 04:24 PM

  7. Piotr Sarnacki
    Piotr Sarnacki

    Robert: Ok, it also works on stable, my mistake. The thing that's wrong is not initializers loading, you just keep railtie definition in wrong place. environment.rb is not loaded when you run rake tasks. Move it to application.rb or to some file inside lib dir and then load in application.rb.

    November 18th, 2010 @ 06:00 PM

  8. Robert Pankowecki
    Robert Pankowecki

    In my real application I keep it under lib/ however my require statement was in environment.rb. Thanks Piotr for the clarification about this. I tested that you are right ans putting the railtie or requiring it in application.rb works fine.

    The lesson here is:
    a) keep you model and all extensions to rails under application.rb
    b) the environment.rb is just to start Rails application. If you want your changes to be visible in rake then put them in application.rb
    c) Despite its name the :environment rake task does not load environment.rb file (I must say that it was unexpected and little confusing a little bit).

    I would expect the responsibilites of those two files to be put in another way
    application.rb - my guess: starts the application
    environment.rb - my guess: loads the whole environment

    And the reality is that they are swapped. What a funny framework we have :-)

    Once again: Thank you Piotr for helping me with that. I think that we mark the bug as invalid.

    It should be mentioned on some page "guides, documenation, an important rails programmer blog, railscasts, whatever..." that rails and application extensions should be put in application.rb . I use the framework since v1.0 and haven't heared about it :-) Good to know that what I did is basically an antipattern.

    November 18th, 2010 @ 11:17 PM

  9. rails
    rails
    • State changed from new to open
    • Tag changed from initializers, rails3.0.1, railties, rake to initializers, rails301, railties, rake

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 19th, 2011 @ 12:00 AM

  10. rails
    rails
    • State changed from open to stale

    February 19th, 2011 @ 12:00 AM

  11. bingbing