activerecord-hackery / squeel

Active Record, improved. Live again :)
http://erniemiller.org/2013/11/17/anyone-interested-in-activerecord-hackery/
MIT License
2.4k stars 214 forks source link

eager loading polymorphic associations #262

Open the8472 opened 11 years ago

the8472 commented 11 years ago

.preload takes keypaths in its DSL form, but it doesn't seem to handle the type discriminators on polymorphic associations:

ModelA.preload do
  [
    something_polymorphic(SpecificTypeB).relation_only_available_on_that_type,
    something_polymorphic.available_on_all
  ]
end
ernie commented 11 years ago

Correct.That's intentional, though. Not really sure what the desired outcome would be on this one?

the8472 commented 11 years ago

UI iterates over instances of ModelA but needs to fetch some additional data from the associations. The views specialize each entry based on the polymorphic type, and the one for SpecificTypeB needs another relation.

So it's to avoid N+1 queries.

This wouldn't be necessary if there was some "take all these objects and bulk-load association X on all of the already-loaded instances" logic. But that doesn't seem to exist as far as I can see.

odiak commented 10 years ago

:+1: I really want this feature.