How does Rails populate the “model_type” field for polymorphic associations?

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

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

I want a Presenter to behave like the parent object it wraps, and I need to override the right method.



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

def create_belongs_to_reflection(association_id, options)
  reflection = create_reflection(:belongs_to, association_id, options, self)

  if options[:polymorphic]
    reflection.options[:foreign_type] ||= reflection.class_name.underscore + "_type"


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

Need Your Help

Ask for user input initially, but immediately proceed with remaining code without waiting for answer


I'm analyzing several different data sets that individually take a while to process. I need to input a bunch of information for each set of data, but the info itself (series of strings) is arbitrar...

Is there any iPhone/iPad UI toolkit such as BWToolkit for Mac apps?

iphone design ipad user-interface toolkit

Just wondering if is there any user interface toolkit for iPhone or iPad such as BWToolkit for creating nice interface for apps.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.