Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

[patch] The polymorphic type field should be subclass name instead of base class name when using polymorphic association with single table inheritance

#2756

For a long time it was a problem for people wanting to use polymorphic association with single table inheritance.
The solution was pointed out (ticket #547 Polymorphic Associations & STI don't record the correct model type, http://dev.rubyonrails.org/ticket/6485, http://dev.rubyonrails.org/ticket/8841).

For example, we should use @owner.class.name.to_s rather than @owner.class.base_class.name.to_s for the record["#{@reflection.options[:as]}_type"] attribute in set_belongs_to_association_for method in active_record/associations/association_proxy.rb.
Beside that, we should consider the namespace problem (full class name or demodulized name) mentioned in ticket #465 Full class name STI not included in eager loading. So @owner.class.sti_name.to_s is more proper than @owner.class.name.to_s.

When we look at the whole active record library, this wrong taken causes many place should be fixed (association_preload.rb, associations.rb, association_proxy.rb, belongs_to_polymorphic_association.rb, has_many_association.rb, has_many_through_association.rb, has_one_association.rb and autosave_association.rb).

I prepared a patch to resolve these situations. This patch should also fix problems mentioned in ticket #2435 "Polymorphic has_one and has_many should set proper polymorphic_type in case ..., #2594 Bug with polymorphic has_one :as pointing to STI record, #2601 Single Table Inheritance, Polymorphic association (self-referential and norma....
The related test cases were fixed too (join_model_test.rb, eager_test.rb, eager_load_includes_full_sti_class_test.rb).
So there were no problems even if we have complicated models like 'Post', 'Tag' and 'Tagging' in test/models.

class Post < ActiveRecord::Base
  has_many :taggings, :as => :taggable

  has_many :tags, :through => :taggings
  has_many :funky_tags, :through => :taggings, :source => :tag
  has_many :super_tags, :through => :taggings

  has_one :tagging, :as => :taggable

  has_many :invalid_taggings, :as => :taggable, :class_name => "Tagging", :conditions => 'taggings.id < 0'
  has_many :invalid_tags, :through => :invalid_taggings, :source => :tag
end

class Tagging < ActiveRecord::Base
  belongs_to :tag, :include => :tagging
  belongs_to :super_tag,   :class_name => 'Tag', :foreign_key => 'super_tag_id'
  belongs_to :invalid_tag, :class_name => 'Tag', :foreign_key => 'tag_id'
  belongs_to :taggable, :polymorphic => true, :counter_cache => true
end

class Tag < ActiveRecord::Base
  has_many :taggings
  has_many :taggables, :through => :taggings
  has_one  :tagging

  has_many :tagged_posts, :through => :taggings, :source => :taggable, :source_type => 'Post'
end

The remain question is this patch will cause problems to plugins which also use 'base_class.name', like 'has_many_polymorphs' as I know.
Maybe more test cases should be added for this path, but I feel that this problem should be solved before rails 3.0.

Reported by tsechingho · June 4th, 2009 @ 11:57 AM

State: stale
Milestone: 3.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to José Valim

    August 9th, 2009 @ 10:01 PM

  2. José Valim
    José Valim
    • Assigned user changed from José Valim to Pratik

    August 9th, 2009 @ 10:08 PM

  3. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  4. Dan Pickett
    Dan Pickett
    • Tag changed from 2.3.2, activerecord, associations, association_preload, association_proxy, autosave_association, belongs_to_polymorphic_association, has_many_association, has_many_through_association, has_one_association, patch, polymorphic, sti to 2.3.2, activerecord, associations, association_preload, association_proxy, autosave_association, belongs_to_polymorphic_association, bugmash, has_many_association, has_many_through_association, has_one_association, patch, polymorphic, sti

    May 9th, 2010 @ 07:27 PM

  5. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Tag changed from 2.3.2, activerecord, associations, association_preload, association_proxy, autosave_association, belongs_to_polymorphic_association, bugmash, has_many_association, has_many_through_association, has_one_association, patch, polymorphic, sti to 232, activerecord, associations, association_preload, association_proxy, autosave_association, belongs_to_polymorphic_association, bugmash, has_many_association, has_many_through_association, has_one_association, patch, polymorphic, sti
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 04:39 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 04:39 PM