This project is archived and is in readonly mode.
ActiveRecord uses wrong class for has_many association
-
Marco Otte-Witte
- Tag changed from activerecord associations, activerecord to activerecord associations, 2.3.5, activerecord
-
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.
-
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 -
Marco Otte-Witte
- Importance changed from to Low
Ok, I tried to fix it with
:class_name => ::Report.namebut that wouldn't fix it of course. Guess ::Report.name is 'Report' not '::Report'
-
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.
-
Marco Otte-Witte
Yes, that's what I meant to say.
-
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">] -
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.
-
Rohit Arondekar
- Tag changed from activerecord associations, 2.3.5, activerecord to 2.3.5, activerecord, associations