How does Rails populate the model_type field for polymorphic associations?

I have an Activity model. This belongs_to :parent, :polymorphic => true .

Does Rails parent.class.name , parent.model_name or something else to populate the parent_type field?

I want the lead to behave as the parent that it wraps, and I need to override the correct method.

Thanks.

+5
source share
1 answer

I am working with Rails 3.0.7 right now, and the polymorphic type is defined in active_record-3.0.7/lib/active_record/association.rb , line 1773.

 def create_belongs_to_reflection(association_id, options) options.assert_valid_keys(valid_keys_for_belongs_to_association) reflection = create_reflection(:belongs_to, association_id, options, self) if options[:polymorphic] reflection.options[:foreign_type] ||= reflection.class_name.underscore + "_type" end reflection end 

So it looks like it calls class_name.underscore and then adds "_type". This may be slightly different for rails 3.1, but this should be a good starting place.

+6
source

All Articles