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.

ActiveRecord uses wrong class for has_many association

#4744

When I use the reportable gem (which defines a class named "Saulabs::Reportable::Report") in a Rails (2.3.5) project that itself defines a "Report" model and an "Account" model that

has_many :reports

I get the error

undefined method `quoted_table_name' for Saulabs::Reportable::Report:Class

/usr/local/Cellar/ruby-enterprise-edition/2010.01/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/reflection.rb:187:in `quoted_table_name'
/usr/local/Cellar/ruby-enterprise-edition/2010.01/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/has_many_association.rb:97:in `construct_sql'
/usr/local/Cellar/ruby-enterprise-edition/2010.01/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations/association_collection.rb:21:in `initialize'
/usr/local/Cellar/ruby-enterprise-edition/2010.01/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `new'
/usr/local/Cellar/ruby-enterprise-edition/2010.01/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/associations.rb:1300:in `reports'
/Users/marcoow/tstapp/app/views/accounts/index.html.erb:11:in `_run_erb_app47views47accounts47index46html46erb'
/Users/marcoow/tstapp/app/views/accounts/index.html.erb:8:in `each'
/Users/marcoow/tstapp/app/views/accounts/index.html.erb:8:in `_run_erb_app47views47accounts47index46html46erb'
/Users/marcoow/tstapp/app/controllers/accounts_controller.rb:7:in `index'

This also occurs when I specify the class name:

has_many :reports, :class_name => ::Report.name

Since I couldn't really come up with a simple test case for this I created a sample app that demonstrates the problem: http://github.com/marcoow/activerecord_bug_sample

Reported by Marco Otte-Witte · June 1st, 2010 @ 02:25 PM

State: invalid
Milestone: none
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. Marco Otte-Witte
    Marco Otte-Witte
    • Tag changed from activerecord associations, activerecord to activerecord associations, 2.3.5, activerecord

    June 1st, 2010 @ 02:26 PM

  2. Neeraj Singh
    Neeraj Singh

    Can you open a ticket with reportable gem if you are still having an issue. It does not seem like it is a rails issue.

    June 25th, 2010 @ 09:34 PM

  3. Andrew White
    Andrew White
    • State changed from new to invalid

    The reportable gem includes Saulabs::Reportable into ActiveRecord::Base which makes Saulabs::Reportable::Report available inside Account. AR::Base#compute_type first looks inside the class to see if the constant is available so you need tell it to reference the top level constant by prefixing it with '::', e.g:

    class Account < ActiveRecord::Base
      has_many :reports, :class_name => '::Report'
    end
    

    June 25th, 2010 @ 10:16 PM

  4. Marco Otte-Witte
    Marco Otte-Witte
    • Importance changed from to Low

    Ok, I tried to fix it with

    :class_name => ::Report.name
    

    but that wouldn't fix it of course. Guess ::Report.name is 'Report' not '::Report'

    June 28th, 2010 @ 09:37 AM

  5. Andrew White
    Andrew White
    • Assigned user set to Andrew White

    The :class_name option should be passed a string '::Report' - by doing as ::Report.name it removes the leading scope operator.

    June 28th, 2010 @ 09:45 AM

  6. Marco Otte-Witte
    Marco Otte-Witte

    Yes, that's what I meant to say.

    June 28th, 2010 @ 09:47 AM

  7. Andrew White
    Andrew White

    Which version of rails are you using?

    This definition for the Account model works for me in 2.3.8:

      class Account < ActiveRecord::Base
        has_many :reports, :class_name => '::Report'
      end
    
      >> Account.first.reports
      => [#<Report id: 1, account_id: 1, created_at: "2010-06-28 08:53:34", updated_at: "2010-06-28 08:53:34">]
    

    June 28th, 2010 @ 10:00 AM

  8. Marco Otte-Witte
    Marco Otte-Witte

    Yep, the version with '::Report' works, the one with ::Report.name does not since that will result in 'Report' which will then result in Reportable's Report class being found since Saulabs::Reportable is included in ActiveRecord::Base.

    I don't think this is specific to any Rails version. I tried it with 2.3.5.

    However, this is not a Rails/ActiveRecord problem - the ticket really is invalid as noted above.

    June 28th, 2010 @ 10:04 AM

  9. Rohit Arondekar
    Rohit Arondekar
    • Tag changed from activerecord associations, 2.3.5, activerecord to 2.3.5, activerecord, associations

    October 7th, 2010 @ 05:21 AM