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.

Polymorphic belongs_to associations error if they have conditions when loading target

#6262

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')

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

State: resolved
Milestone: none
Assigned to: Aaron Patterson Aaron Patterson
Importance: Low

Activity

  1. Ernie Miller
    Ernie Miller
    • 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.

    January 7th, 2011 @ 05:51 PM

  2. Jon Leighton
    Jon Leighton
    • Importance changed from to Low

    Hiya,

    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.

    Cheers,
    Jon

    January 11th, 2011 @ 03:59 PM

  3. Jon Leighton
    Jon Leighton
    • State changed from new to resolved

    Hey,

    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...

    Cheers

    January 11th, 2011 @ 08:13 PM