Rails runner LoadError on observers. #5847

Closed
chengguangnan opened this Issue Apr 14, 2012 · 5 comments

2 participants

@chengguangnan
rails new test_app
rails g model car
rails g observer car
# add :car_observer to config/application.rb
rails r 'Car.all'

.rvm/gems/ruby-1.9.3-p0/gems/railties-3.2.3/lib/rails/commands/runner.rb:53:in `eval': Expected workspace/word_test/app/models/car.rb to define Car (LoadError)
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `each'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/dependencies.rb:190:in `const_missing'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:229:in `block in constantize'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in `each'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/inflector/methods.rb:228:in `constantize'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:210:in `observed_class'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:203:in `observed_classes'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:223:in `observed_classes'
    from .rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.3/lib/active_record/observer.rb:96:in `observed_classes'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:219:in `initialize'
    from .rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/singleton.rb:141:in `new'
    from .rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/singleton.rb:141:in `block in instance'
    from <internal:prelude>:10:in `synchronize'
    from .rvm/rubies/ruby-1.9.3-p0/lib/ruby/1.9.1/singleton.rb:139:in `instance'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:86:in `instantiate_observer'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:59:in `block in instantiate_observers'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:59:in `each'
    from .rvm/gems/ruby-1.9.3-p0/gems/activemodel-3.2.3/lib/active_model/observing.rb:59:in `instantiate_observers'
    from .rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.3/lib/active_record/railtie.rb:111:in `block (2 levels) in <class:Railtie>'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from .rvm/gems/ruby-1.9.3-p0/gems/activesupport-3.2.3/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from .rvm/gems/ruby-1.9.3-p0/gems/activerecord-3.2.3/lib/active_record/base.r```
@kennyj

I guess that this issue is related to #4045.

@kennyj

Umm...

The follwoing testcase is passed on master, but isn't passed on 3-2-stable.

    def test_load_activerecord_base_when_we_use_observers
      Dir.chdir(app_path) do
        `bundle exec rails g model user`
        `bundle exec rake db:migrate`
        `bundle exec rails g observer user`
      end
      add_to_config "config.active_record.observers = :user_observer"

      output = Dir.chdir(app_path) { `bundle exec rails r "p User.count"` }
      assert_equal "0", output.strip
    end

I guess that ActiveRecord::Base is loaded initially on master.
Maybe d0bb43d

@kennyj

@guangnan
Please add the following code on config/application.rb, and test it :-)

config.after_initialize do
  _ = ActiveRecord::Base if config.active_record.observers
end

p.s. AMPAMMAN ?

@chengguangnan

@kennyj, your after_initialize fixed it. Anpanman thanks you!

@kennyj

I sent the PR #5879 :-)

@kennyj kennyj added a commit to kennyj/rails that referenced this issue Apr 24, 2012
@kennyj kennyj Testcase for #5847 and #4045. 65fa512
@kennyj kennyj added a commit to kennyj/rails that referenced this issue Apr 24, 2012
@kennyj kennyj Fix #5847 and #4045. Force load AR::Base before loading an applicatio…
…n model.
1e2b7a8
@kennyj kennyj added a commit to kennyj/rails that referenced this issue Apr 26, 2012
@kennyj kennyj Fix #5847 and #4045. Force load AR::Base before loading an applicatio…
…n model.
81af288
@kennyj kennyj closed this in 627f89b Apr 26, 2012
@kennyj kennyj added a commit to kennyj/rails that referenced this issue May 29, 2012
@kennyj kennyj Add a testcase for #5847. 8d01c61
@kennyj kennyj added a commit to kennyj/rails that referenced this issue May 29, 2012
@kennyj kennyj Fix #5847 and #4045 on master. c0ba0f0
@lukaszx0 lukaszx0 added a commit to square/rails that referenced this issue Mar 20, 2014
@drogus drogus Bring back config.action_view.cache_template_loading [#5847 state:res…
…olved]
a4a9e53
@lukaszx0 lukaszx0 added a commit to square/rails that referenced this issue Mar 20, 2014
@drogus drogus Bring back config.action_view.cache_template_loading [#5847 state:res…
…olved]
56a9b79
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment