Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

When using ActiveRecord::Associations outside of Rails, a NameError is thrown

#2577

Here's an example irb session:

irb(main):002:0> require 'rubygems' => true irb(main):003:0> require 'active_record' => true irb(main):004:0> ActiveRecord => ActiveRecord irb(main):005:0> ActiveRecord::Associations NameError: uninitialized constant ActiveRecord::Associations

from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:440:in `load_missing_constant'
from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.2/lib/active_support/dependencies.rb:80:in `const_missing'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:3144
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:3137:in `class_eval'
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:3137
from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/associations.rb:2
from (irb):6
from :0

This happens because ruby loads the associations.rb file, which references ActiveRecordError before defining the Associations module. ActiveRecordError is defined in base.rb, which tries to include the Association module before it's been defined.

Patch attached.

Reported by danielh · April 28th, 2009 @ 03:20 PM

State: open
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Elijah Miller
    Elijah Miller

    +1 Applies cleanly to 2-3-stable and works, all tests pass.

    No test cases are provided for this issue but here is a command to test. Run within activerecord/lib.

    ruby -e "require 'activerecord'; ActiveRecord::Associations; puts 'pass'"

    Before patch:

    ./../../activesupport/lib/active_support/dependencies.rb:440:in `load_missing_constant': uninitialized constant ActiveRecord::Associations (NameError)
        from ./../../activesupport/lib/active_support/dependencies.rb:80:in `const_missing'
        from ./active_record/base.rb:3139
        from ./active_record/base.rb:3132:in `class_eval'
        from ./active_record/base.rb:3132
        from ./active_record/associations.rb:2
        from -e:1
    

    After patch:

    pass
    

    June 6th, 2009 @ 04:43 PM

  2. Elijah Miller
    Elijah Miller

    For now a simple work-around is to reference ActiveRecord::ActiveRecordError before ActiveRecord::Associations

    require 'activerecord'
    ActiveRecord::ActiveRecordError
    # now you can use ActiveRecord::Associations
    

    June 6th, 2009 @ 05:34 PM

  3. Murray Steele
    Murray Steele
    • Tag changed from 2.3.2, activerecord, association to 2.3.x, 3.0, activerecord, association

    The same is true if you try to use ActiveRecord::Validations. The similarities are that both active_record/validations.rb and active_record/associations.rb both define ActiveRecordError subclasses before they define their Validations and Associations modules.

    Note really sure what the fix is. It also affects 3.0.

    August 5th, 2009 @ 05:32 PM

  4. ggwang
    ggwang
    • Tag changed from 2.3.x, 3.0, activerecord, association to 2, 3, activerecord, association

    It seems to be same problem, the following is #3528 Regarding to #2577 , something wrong with my associations.rb I added just now:

    I am intending to have a try to use CMS ansuz(http://www.ansuzcms.com/). But Ijust can not make it get to work. When ever start up the webtick server, or try to migrate its database(yml setting finished), it shows:

    => Booting WEBrick => Rails 2.3.5 application starting on http://0.0.0.0:3000 /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:440:in load_missing_constant': uninitialized constant ActiveRecord::Associations (NameError)

    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in `const_missing'
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3154
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3147:in `class_eval'
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3147
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:2
    from /home/ggwang/ansuz/lib/isotope11/active_record_extensions.rb:2
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/ansuz/config/environments/development.rb:24:in `load_environment'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:386:in `load_environment'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:379:in `load_environment'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:137:in `process'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `send'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `run'
    from /home/ggwang/ansuz/config/environment.rb:39
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/commands/server.rb:84
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from script/server:3
    

    ggwang@ggwang-laptop:~/ansuz$ ruby script/server
    => Booting WEBrick => Rails 2.3.5 application starting on http://0.0.0.0:3000 /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:440:in load_missing_constant': uninitialized constant ActiveRecord::Associations (NameError)

    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in `const_missing'
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3154
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3147:in `class_eval'
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3147
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:2
    from /home/ggwang/ansuz/lib/isotope11/active_record_extensions.rb:2
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/ansuz/config/environments/development.rb:24:in `load_environment'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:386:in `load_environment'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/kernel/reporting.rb:11:in `silence_warnings'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:379:in `load_environment'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:137:in `process'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `send'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/initializer.rb:113:in `run'
    from /home/ggwang/ansuz/config/environment.rb:39
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:521:in `new_constants_in'
    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:156:in `require'
    from /home/ggwang/.gem/ruby/1.8/gems/rails-2.3.5/lib/commands/server.rb:84
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from script/server:3
    

    I took referenced to the ticket #2577 When using ActiveRecord::Associations outside of Rails, a NameError is thrown and thought maybe that was the problem. I tried to use the patch file attached but could not make it work. The following is my gem list:

        *** LOCAL GEMS ***
    

    actionmailer (2.3.5, 2.2.2)
    actionpack (2.3.5, 2.3.2, 2.2.2)
    activerecord (2.3.5, 2.3.2, 2.2.2)
    activeresource (2.3.5, 2.2.2)
    activesupport (2.3.5, 2.3.4, 2.3.2, 2.2.2)
    BlueCloth (1.0.1)
    calendar_date_select (1.15)
    cmdparse (2.0.2)
    datanoise-actionwebservice (2.3.2)
    facets (2.4.5)
    maruku (0.5.9)
    mislav-will_paginate (2.2.3)
    mocha (0.9.8)
    mysql (2.8.1)
    rack (1.0.1)
    rails (2.3.5, 2.2.2)
    rake (0.8.7)
    rdoc (2.4.3)
    RedCloth (4.2.2)
    rubygems-update (1.3.5)
    rubyist-aasm (2.0.7)
    sqlite3-ruby (1.2.5)
    syntax (1.0.0)
    webgen (0.5.10)
    will_paginate (2.3.11, 2.2.2)

    And, I followed the ticket to test in irb and it shows:

    :~/ansuz$ ruby -e "require 'rubygems';require 'active_record'; ActiveRecord::Associations; puts 'pass'" /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:440:in load_missing_constant': uninitialized constant ActiveRecord::Associations (NameError)

    from /home/ggwang/.gem/ruby/1.8/gems/activesupport-2.3.5/lib/active_support/dependencies.rb:80:in `const_missing'
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3154
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3147:in `class_eval'
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:3147
    from /home/ggwang/.gem/ruby/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:2
    from -e:1
    

    Is it really a bug of rails? Can anyone help me?

    December 1st, 2009 @ 12:03 AM

  5. seraphim
    seraphim

    I ran into the same problem... anyone solved the issue?

    December 22nd, 2009 @ 06:31 AM

  6. PeppyHeppy
  7. Rizwan Reza
    Rizwan Reza
    • Tag changed from 2, 3, activerecord, association to 2, 3, 3.0, activerecord, association
    • Assigned user set to Pratik

    I can reproduce this in Rails 3 pre.

    January 18th, 2010 @ 01:13 PM

  8. Rhett Sutphin
    Rhett Sutphin

    Seems to me that the lowest-impact fix would be to move ActiveRecordError out of base.rb and into its own file. That would break the autoload cycle (ActiveRecordError loads from base.rb, base.rb loads Associations, Associations loads ActiveRecordError).

    In any case, another workaround is to explicitly require 'active_record/base' after requiring 'active_record'.

    April 3rd, 2010 @ 02:28 AM

  9. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  10. Andrea Campi
    Andrea Campi
    • Tag changed from 2, 3, 3.0, activerecord, association to 2-3-stable, 3, 3.0, activerecord, association
    • Importance changed from to

    October 16th, 2010 @ 11:42 PM

  11. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from 2-3-stable, 3, 3.0, activerecord, association to 2-3-stable, 3, 30, activerecord, association

    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.

    February 2nd, 2011 @ 04:28 PM

  12. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:28 PM

  13. Betelgeuse
    Betelgeuse
    • State changed from stale to open

    [state:open]

    irb(main):008:0> ActiveRecord::Associations
    NameError: uninitialized constant ActiveRecord::Associations
            from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/dependencies.rb:466:in `load_missing_constant'
            from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/dependencies.rb:106:in `const_missing'
            from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:3207
            from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:3200:in `class_eval'
            from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.10/lib/active_record/base.rb:3200
            from /usr/local/lib/ruby/gems/1.8/gems/activerecord-2.3.10/lib/active_record/associations.rb:2
            from (irb):8
    

    February 2nd, 2011 @ 05:35 PM

  14. bingbing
  15. daviejacob
    daviejacob

    After read this post i want to keep coming here again and again.Thanks a lot. buy facebook fans

    March 28th, 2011 @ 04:42 PM