This project is archived and is in readonly mode.
Eager loading load association for every STI subclass when it is declared in superclass
-
Frederick Cheung
Well [http://dev.rubyonrails.org/ticke...][this ticket] is why the current behaviour is what it is. I can't think off the top of my head whether that the ticket and what you've noticed can easily be reconciled.
Whatever it is, the fix is likely to be in preload_one_association where it does records.group_by(&:class); if it can be determined when this partitoning is not necessary and fall back.
-
Frederick Cheung
I believe this does the trick. Would be good for those bitten by this to try it out
-
Tarmo Tänav
There seems to be an unused "class_to_reflection = {}" there, planning to cache the reflection for each cache?
-
Frederick Cheung
D'oh, somehow manager to produce a patch with not the final version (and mix that in with a patch from another ticket I'd applied locally to test - no idea how that happened!). I think I have untangled my git-tardedness now
-
Denis Barushev
- Title changed from Eager loading load association for every STI subclass when it is declared in superclass to [PATH] Eager loading load association for every STI subclass when it is declared in superclass
I'm not good familiar with Rails sources, but I fixed this problem. See path.
Manual test:
class Asset < ActiveRecord::Base belongs_to :user end class Image < Asset end class Audio < Asset end class Video < Asset end class Asset1 < ActiveRecord::Base belongs_to :user end class Image1 < Asset1 end class Audio1 < Asset1 belongs_to :user end class Video1 < Asset1 end>> Asset.destroy_all >> Asset1.destroy_all >> Audio.create :user => User.first >> Audio.create :user => User.last >> Video.create :user => User.first >> Image.create :user => User.first >> Audio1.create :user => User.first >> Audio1.create :user => User.last >> Video1.create :user => User.first >> Image1.create :user => User.first >> Asset.all :include => :user >> Asset1.all :include => :userSQL-queries:
Asset Load (0.003225) SELECT * FROM "assets" User Load (0.001348) SELECT * FROM "users" WHERE ("users".id IN ('1','2')) Asset1 Load (0.001352) SELECT * FROM "asset1s" User Load (0.000919) SELECT * FROM "users" WHERE ("users".id IN ('1','2')) User Load (0.000703) SELECT * FROM "users" WHERE ("users".id IN ('1'))How test this behavior in Test::Unit?
-
Frederick Cheung
See the patch I attached :-)
-
Denis Barushev
- Title changed from Eager loading load association for every STI subclass when it is declared in superclass to [PATH] Eager loading load association for every STI subclass when it is declared in superclass
Oops, I haven't seen your patches. Lighthouse doesn't have intuitive interface.
Sorry :)
-
Repository
- State changed from new to resolved
- Title changed from [PATH] Eager loading load association for every STI subclass when it is declared in superclass to Eager loading load association for every STI subclass when it is declared in superclass
(from [236f0bb67adecbc1e6dac5258e4a8cb310ffd7a4]) When preloading group by reflection rather than by class [#125 state:resolved]
This avoids extra queries when several subclasses inherit the association
from their parent class, while still coping with subclasses redefining
associations.
Signed-off-by: Pratik Naik
