Imported from Lighthouse. Original ticket at: http://rails.lighthouseapp.com/projects/8994/tickets/6493
Created by Ludo van den Boom - 2011-03-01 15:23:06 UTC
Copied most of the info from ticket #5074. The problem I encounter is the same as in that ticket, except I don't have issues with rails console (that has already been resolved in ticket #5074), but with running rake tasks. Attached patch resolves this issue for rake tasks as well.
I am using Ruby 1.8.7 and Rails 3.0.5
Steps to reproduce
1) rails new foo
2) cd foo
3) rails generate model bar
4) rails generate observer bar
5) rake db:migrate
6) in config/application.rb file:
config.active_record.observers = :bar_observer
6) touch lib/tasks/qux.rake
7) in lib/tasks/qux.rake:
task :qux => :environment do
8) rake qux
Results in an error:
Expected /Users/ludo/src/foo/app/models/bar.rb to define Bar
Attachments saved to Gist: http://gist.github.com/971791
Why was this closed? I still get this in Rails 3.1.1.
This bug still occurs with Rails 3.2.1 and Ruby 1.9.3p0. Needs to be reopened and the fix introduced into the mainline.
Problem confirmed with Rails 3.2.2 and ruby 1.9.3p0
Hi everyone. I have the same problem. All models which have observers return the same error in custom rake tasks.
ruby 1.9.3p0 (2011-10-30 revision 33570)
I have a model User and observer: UserObserver.
config.active_record.observers = :user_observer
After that i created simple rake task:
namespace :foo_task do
desc "Just for test"
task :get_users_count => :environment do
When i'm trying to run this rake task it shows:
Expected /var/www/foobarapp/app/models/user.rb to define User
Is there some way to fix this problem?
Thanks for help.
I have the same problem. It works when I run the code in rails console, but when I try the rake task, it has the same error as described in the ticket. I am using ruby 1.8.7 and rails 3.2.8.
@jiongye Rails 2.3 does not receive support of any kind any more, just so you know.
@steveklabnik sorry, I had a typo. I mean v3.2.8.
Also when I added "ActiveRecord::Base" inside my rake task, everything works.
Awesome! Thanks for clarifying.
I'm going to re-open this since it was only closed in the first place due to the automatic import.
The rake task is a red herring, it is enough to invoke rails runner Bar.
I think the fix can be related with the eager_load work. @josevalim could you confirm?
One problem here is that the error message is misleading. The underlying issue is that there is circular autoloading going on: autoloading Bar triggers autoloading BarObserver triggers autoloading Bar.
In master I just pushed b33700f to give at least a better error message.
That does not solve the issue itself, though, this is just a followup.
Hm, also, I wonder how this will interact with the lazy observer stuff too.
Can confirm this is an issue on 3.2.9. Adding "ActiveRecord::Base" works. Any plans to add the patch?
This seems to be fixed in Rails 4.0 but not ported back to Rails 3.2.x. 9fa3926
@espen there is a pull request to backport that fix #7695
backport runner fixes thanks @benolee @kennyj
Backport cc7dd66 and c0ba0f0, which fix the runner
loading issue from issue #7683
closes #7695 #882
@espen could you try 3-2-stable? #7695 was merged.
@rafaelfranca Not using observers in my app anymore but hopefully this fixes the problem for others.