This project is archived and is in readonly mode.

#6262 ✓resolved
Ernie Miller

Polymorphic belongs_to associations error if they have conditions when loading target

Reported by Ernie Miller | January 7th, 2011 @ 05:49 PM

The title pretty much describes the problem -- if you define a polymorphic belongs_to association and it has conditions on it, those conditions cause a NameError when you try to access the target object, of the style saying "hey, there's no such thing as a Note::Notable".

This is because a reflection's sanitized_conditions method calls klass, and klass calls class_name, and class_name calls derive_class_name, and, well, derive_class_name can't really derive a class name properly, in the case of a polymorphic belongs_to, because the reflection doesn't have all the information it needs -- the association proxy does.

Anyway, that's about it. Attached are a couple of patches including tests for both master and 3-0-stable.

Thanks for reading!

(assigning to tenderlove because this ticket needs some tender lovin')

Comments and changes to this ticket

  • Ernie Miller

    Ernie Miller January 7th, 2011 @ 05:51 PM

    • Tag changed from patch test association_proxy belongs_to polymorphic to association_proxy, belongs_to, patch, polymorphic, test

    Attaching the other patch and fixing my tags because I'm a sad, sad excuse for a Lighthouse user.

  • Jon Leighton

    Jon Leighton January 11th, 2011 @ 03:59 PM

    • Importance changed from “” to “Low”


    Can you check if this is still an issue against current master? A recent change I made (770e6893b9f2aaaebe3de10576931dc7194451bc) basically bypasses the conditions method and adds everything to a scope which is used instead. So I suspect this might solve your problem.

    I'd be happy for this to be added to 3-0-stable though. BTW you patch contains a hard tab, which should be converted to a double space.


  • Jon Leighton

    Jon Leighton January 11th, 2011 @ 08:13 PM

    • State changed from “new” to “resolved”


    I see via Twitter that Aaron applied your patch :)

    It turns out that the test does pass straight off since that change I mentioned, so I've added a commit to my own fork which just deletes the conditions method (but not the test). I'll get Aaron to merge it...


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>