Skip to content
This repository

Observer loading crashes in script runners #7683

Closed
chrono opened this Issue · 9 comments

5 participants

Martin Ringehahn Toshinori Kajihara Steve Klabnik benolee Rafael Mendonça França
Martin Ringehahn
% rails -v      
Rails 3.2.8

This is basically the bug fixed in https://rails.lighthouseapp.com/projects/8994/tickets/5074 for the console, but still present for the script runner context.

Steps to reproduce:

rails new ./foo
rails generate model bar
rails generate observer bar
rake db:migrate

in application.rb add

config.active_record.observers = :bar_observer

create scripts/bar.rb with

Bar.create

then run the script

rails runner scripts/bar.rb

now observe this nice stacktrace:

/home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:503:in `load_missing_constant': Expected /home/martin/foo/app/models/bar.rb to define Bar (LoadError)
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `each'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `const_missing'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/inflector/methods.rb:230:in `block in constantize'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/inflector/methods.rb:229:in `each'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/inflector/methods.rb:229:in `constantize'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/core_ext/string/inflections.rb:54:in `constantize'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:210:in `observed_class'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:203:in `observed_classes'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:223:in `observed_classes'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/observer.rb:96:in `observed_classes'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:219:in `initialize'
    from /home/martin/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/singleton.rb:141:in `new'
    from /home/martin/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/singleton.rb:141:in `block in instance'
    from <internal:prelude>:10:in `synchronize'
    from /home/martin/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/singleton.rb:139:in `instance'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:86:in `instantiate_observer'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:59:in `block in instantiate_observers'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:59:in `each'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activemodel-3.2.8/lib/active_model/observing.rb:59:in `instantiate_observers'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railtie.rb:117:in `block (2 levels) in <class:Railtie>'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:43:in `block in run_load_hooks'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:42:in `each'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/lazy_load_hooks.rb:42:in `run_load_hooks'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/base.rb:721:in `<top (required)>'
    from /home/martin/foo/app/models/bar.rb:1:in `<top (required)>'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `load'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:469:in `block in load_file'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:639:in `new_constants_in'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:468:in `load_file'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:353:in `require_or_load'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:502:in `load_missing_constant'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:192:in `block in const_missing'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `each'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:190:in `const_missing'
    from script/bar.rb:1:in `<top (required)>'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/runner.rb:51:in `eval'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands/runner.rb:51:in `<top (required)>'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:64:in `require'
    from /home/martin/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.8/lib/rails/commands.rb:64:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
Toshinori Kajihara
Collaborator

I think this issue was fixed by my commits on master.
cc7dd66 and c0ba0f0

Could you test this on master ?

Toshinori Kajihara
Collaborator

If you change scripts/bar.rb to

require 'active_record/base' # added
Bar.create

and the problem is gone, I guess my above commit fixes your one.

Steve Klabnik
Collaborator

@kennyj it might be a good idea to backport those to 3-2-stable, especially since observers are getting pulled out into a plugin.

benolee

It looks like cc7dd66 needs f8e7904 as a pre-requisite when backporting to 3-2-stable.

benolee

I tried to backport the fixes. Please let me know if there's anything wrong with my pull request and I'll try to fix it -- I'm an amateur!

benolee benolee referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Martin Ringehahn

I've tested both rails@64146cf (the current master) and benolee@b4c228d (the pull request)
-- fixed in both.

I'm already using the workaround as suggested by @kennyj

Toshinori Kajihara
Collaborator
Toshinori Kajihara
Collaborator

I commented to @benolee's PR.

benolee benolee referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
benolee benolee referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Rafael Mendonça França

I;m closing this one in favor of #7695

Arun Agrawal arunagw referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Arun Agrawal arunagw referenced this issue from a commit in arunagw/rails
Arun Agrawal arunagw backport runner fixes thanks @benolee @kennyj
Backport cc7dd66 and c0ba0f0, which fix the runner
loading issue from issue #7683


closes #7695 #882
69ad784
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.