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.

Preloading polymorphic association doesn't work with namespaced models

#5023

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

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

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

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.

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

State: stale
Milestone: none
Assigned to: nobody
Importance: none

Activity

  1. Santiago Pastorino
    Santiago Pastorino
    • 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.

    February 2nd, 2011 @ 05:01 PM

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

    February 2nd, 2011 @ 05:01 PM

  3. Brian Samson
    Brian Samson

    +1

    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.

    March 10th, 2011 @ 06:45 PM