This project is archived and is in readonly mode.
ActiveRecord uses wrong class for has_many association
Reported by Marco Otte-Witte | June 1st, 2010 @ 02:25 PM
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
Comments and changes to this ticket
-
Marco Otte-Witte June 1st, 2010 @ 02:26 PM
- Tag changed from activerecord associations, activerecord to activerecord associations, 2.3.5, activerecord
-
Neeraj Singh June 25th, 2010 @ 09:34 PM
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.
-
Andrew White June 25th, 2010 @ 10:16 PM
- 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
-
Marco Otte-Witte June 28th, 2010 @ 09:37 AM
- 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'
-
Andrew White June 28th, 2010 @ 09:45 AM
- 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.
-
Andrew White June 28th, 2010 @ 10:00 AM
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">]
-
Marco Otte-Witte June 28th, 2010 @ 10:04 AM
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.
-
Rohit Arondekar October 7th, 2010 @ 05:21 AM
- Tag changed from activerecord associations, 2.3.5, activerecord to 2.3.5, activerecord, associations
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>