This project is archived and is in readonly mode.
has_many :class will nest within parent class... no override
-
Brian Cardarella
A simple workaround to fix this:
..../activerecord-2.0.2/lib/active_record/reflection.rb:124
def klass
@klass ||= active_record.send(:compute_type, class_name)
end
change to:
def klass
@klass ||= options[:class_name].constantize || active_record.send(:compute_type, class_name)
end
If you dig into the class_name method it does check the options[:class_name] again so maybe this should be removed. But this will use whatever class you specify in a has_many without nesting it into the parent class of the current model.
-
Brian Cardarella
Small correction:
def klass
@klass ||= (options[:class_name].constantize rescue nil) || active_record.send(:compute_type, class_name)
end
Make this change to catch if options[:class_name] is nil or not a constant.
Again, this is not the most elegant way but it seems to work.
-
Jeremy Kemper
This is so the association can reference a sibling class without needing an explicit :class_name
module UserStuff class User < ActiveRecord::Base has_many :resumes end class Resume < ActiveRecord::Base end end -
Brian Cardarella
Jeremy,
I understand this but when you explicitly define a class_name it will still treat the association as a sibling class instead of referencing the user defined class_name. ActiveRecord should not assume that a user defined class_name is a sibling class.
-
Jeremy Kemper
Why not? The behavior is consistent either way.
-
Brian Cardarella
Because sometimes a child model may not be a sibling of a parent model. My original post:
class Resource::Resume < ActiveRecord::Base; end class User::Candidate < ActiveRecord::Base has_many :resumes, :class_name => "Resource::Resume" endThis will raise warnings. Note that I am using nested models. The User::Candidate model will refer to the :resumes association with User::Resource::Resume even after I specify the exactly class name. The proper class name is Resource::Resume.
Again, if I am explicitly specifying the class name why does AR need to do any work guessing any additional parent classes? That should only be necessary when I do not explicitly specify a class name.
-
josh
- State changed from new to stale
- Tag cleared.