diff options
author | Ryuta Kamizono <kamipo@gmail.com> | 2017-11-07 08:44:44 +0900 |
---|---|---|
committer | Ryuta Kamizono <kamipo@gmail.com> | 2017-11-07 08:58:19 +0900 |
commit | 3f1695bb9c008b7cb1840e09e640f3ec0c59a564 (patch) | |
tree | 10855663d1a1035da978e623637d318557e3c320 | |
parent | 9042ced124874a200b134ec3a4441ab6e2c1be73 (diff) | |
download | rails-3f1695bb9c008b7cb1840e09e640f3ec0c59a564.tar.gz rails-3f1695bb9c008b7cb1840e09e640f3ec0c59a564.tar.bz2 rails-3f1695bb9c008b7cb1840e09e640f3ec0c59a564.zip |
Remove useless `associated_records_by_owner`
`associated_records_by_owner` had returned customizing result before
calling `associate_records_to_owner` for through association subclasses.
Since #22115, `associate_records_to_owner` is called in the method and
not returned owner and result pairs. Removing the method will reduce
method call and block call nesting.
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 22 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/through_association.rb | 4 |
2 files changed, 10 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index b0c41c7d5f..e77761692d 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -17,8 +17,14 @@ module ActiveRecord end def run(preloader) - associated_records_by_owner(preloader) do |owner, records| - associate_records_to_owner(owner, records) + records = load_records do |record| + owner = owners_by_key[convert_key(record[association_key_name])] + association = owner.association(reflection.name) + association.set_inverse_instance(record) + end + + owners.each do |owner| + associate_records_to_owner(owner, records[convert_key(owner[owner_key_name])] || []) end end @@ -33,18 +39,6 @@ module ActiveRecord reflection.join_foreign_key end - def associated_records_by_owner(preloader) - records = load_records do |record| - owner = owners_by_key[convert_key(record[association_key_name])] - association = owner.association(reflection.name) - association.set_inverse_instance(record) - end - - owners.each_with_object({}) do |owner, result| - yield(owner, records[convert_key(owner[owner_key_name])] || []) - end - end - def associate_records_to_owner(owner, records) raise NotImplementedError end diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb index 1bb7e98231..b1813ba66b 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -12,7 +12,7 @@ module ActiveRecord reflection.source_reflection end - def associated_records_by_owner(preloader) + def run(preloader) already_loaded = owners.first.association(through_reflection.name).loaded? through_scope = through_scope() reflection_scope = target_reflection_scope @@ -43,7 +43,7 @@ module ActiveRecord result.sort_by! { |rhs| preload_index[rhs] } if reflection_scope.order_values.any? result.uniq! if reflection_scope.distinct_value end - yield(owner, result) + associate_records_to_owner(owner, result) end end |