diff options
author | Frederick Cheung <frederick.cheung@gmail.com> | 2008-05-07 00:12:17 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2008-05-11 22:14:07 +0100 |
commit | 236f0bb67adecbc1e6dac5258e4a8cb310ffd7a4 (patch) | |
tree | aaca136f3ba80f589e24595cd953f064697ef0bb /activerecord/lib/active_record/association_preload.rb | |
parent | 0cbdd96c349c56c47299ca3c2bfa5d8c4f5e5a11 (diff) | |
download | rails-236f0bb67adecbc1e6dac5258e4a8cb310ffd7a4.tar.gz rails-236f0bb67adecbc1e6dac5258e4a8cb310ffd7a4.tar.bz2 rails-236f0bb67adecbc1e6dac5258e4a8cb310ffd7a4.zip |
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 <pratiknaik@gmail.com>
Diffstat (limited to 'activerecord/lib/active_record/association_preload.rb')
-rw-r--r-- | activerecord/lib/active_record/association_preload.rb | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/association_preload.rb b/activerecord/lib/active_record/association_preload.rb index 660d7647c7..a3d1f12b03 100644 --- a/activerecord/lib/active_record/association_preload.rb +++ b/activerecord/lib/active_record/association_preload.rb @@ -31,12 +31,12 @@ module ActiveRecord private def preload_one_association(records, association, preload_options={}) - reflection = reflections[association] - raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection - - # Not all records have the same class, so group then preload. - records.group_by(&:class).each do |klass, records| - reflection = klass.reflections[association] + class_to_reflection = {} + # Not all records have the same class, so group then preload + # group on the reflection itself so that if various subclass share the same association then we do not split them + # unncessarily + records.group_by {|record| class_to_reflection[record.class] ||= record.class.reflections[association]}.each do |reflection, records| + raise ConfigurationError, "Association named '#{ association }' was not found; perhaps you misspelled it?" unless reflection send("preload_#{reflection.macro}_association", records, reflection, preload_options) end end |