This project is archived and is in readonly mode.
Railtie initializers are not executed in rake tasks
Reported by Robert Pankowecki | October 28th, 2010 @ 03:51 PM
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
Comments and changes to this ticket
-
Jeff Kreeftmeijer November 1st, 2010 @ 05:04 PM
- Tag cleared.
- Importance changed from to Low
Automatic cleanup of spam.
-
Matthew Ratzloff November 12th, 2010 @ 05:38 PM
I'm having this issue, too. Anyone have a workaround?
-
Robert Pankowecki November 14th, 2010 @ 04:23 PM
- Tag set to initializers, railties, rake
The workaround is to execute the railtie initializers before in the rake task
-
Robert Pankowecki November 14th, 2010 @ 04:24 PM
- Tag changed from initializers, railties, rake to initializers, rails3.0.1, railties, rake
-
Robert Pankowecki November 18th, 2010 @ 11:33 AM
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.
-
Piotr Sarnacki November 18th, 2010 @ 04:24 PM
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
-
Piotr Sarnacki November 18th, 2010 @ 06:00 PM
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.
-
Robert Pankowecki November 18th, 2010 @ 11:17 PM
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 environmentAnd 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.
-
rails February 19th, 2011 @ 12:00 AM
- 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.
-
rails February 19th, 2011 @ 12:00 AM
- State changed from open to stale
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>