This project is archived and is in readonly mode.

#5023 ✓stale
David Doan

Preloading polymorphic association doesn't work with namespaced models

Reported by David Doan | June 30th, 2010 @ 11:40 PM

Association preload tries to look up included polymorphic models by base_class.sti_name instead of, which differs with namespaced models. (sti_name drops the namespace.) BelongsToPolymorphic automatically creates the association with type =, so looking it up with base_class.sti_name returns nil.

module Namespace
  class ModelOne
    belongs_to :polymorphic_relationship, :polymorphic => true

  class ModelTwo
    has_one :model_one, :as => :polymorphic_relationship

model_two = Namespace::ModelTwo.create(:id => 1)
model_one = Namespace::ModelOne.create(:id => 1, :polymorphic_relationship => model_two)

model_two = Namespace::ModelTwo.find_by_id(1)
model_two.model_one # (is present)

model_two = Namespace::ModelTwo.find_by_id(1, :include => :model_one)
model_two.model_one # (is nil)

Attached is a patch to AssociationPreload to fix this.

Comments and changes to this ticket

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • State changed from “new” to “open”

    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.

  • Santiago Pastorino

    Santiago Pastorino February 2nd, 2011 @ 05:01 PM

    • State changed from “open” to “stale”
  • Brian Samson

    Brian Samson March 10th, 2011 @ 06:45 PM


    This is still a bug in rails 2.3.11, as it has been since around 2.3.5

    I've attached a patch for it in 2.3.11 that we use. I think it should be included in the next stable 2.3 release as well for people who aren't updating to rails 3.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>

People watching this ticket