This project is archived and is in readonly mode.
Railtie initializers are not executed in rake tasks
-
Matthew Ratzloff
I'm having this issue, too. Anyone have a workaround?
-
Robert Pankowecki
- Tag set to initializers, railties, rake
The workaround is to execute the railtie initializers before in the rake task
-
Robert Pankowecki
- Tag changed from initializers, railties, rake to initializers, rails3.0.1, railties, rake
-
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) endI've got only one initializer in definied in my Railtie.
I hope this will help unit the issue is resolved.
-
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
-
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.
-
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 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
- 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
- State changed from open to stale