From d9a7f86666a8f4345fc2b4680ec9b407294a6cc0 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Fri, 11 Oct 2013 18:02:20 -0700 Subject: push up the loaded target checking --- .../active_record/associations/join_dependency.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'activerecord/lib/active_record/associations') diff --git a/activerecord/lib/active_record/associations/join_dependency.rb b/activerecord/lib/active_record/associations/join_dependency.rb index 50bd2fbe8e..6457880e65 100644 --- a/activerecord/lib/active_record/associations/join_dependency.rb +++ b/activerecord/lib/active_record/associations/join_dependency.rb @@ -216,6 +216,12 @@ module ActiveRecord if node.reflection.collection? other = ar_parent.association(node.reflection.name) other.loaded! + else + if ar_parent.association_cache.key?(node.reflection.name) + model = ar_parent.association(node.reflection.name).target + construct(model, node, row, rs) + next + end end next if row[node.aliased_primary_key].nil? @@ -226,19 +232,17 @@ module ActiveRecord end end - def construct_model(record, join_part, row) - if join_part.reflection.collection? - model = join_part.instantiate(row) - other = record.association(join_part.reflection.name) + def construct_model(record, node, row) + model = node.instantiate(row) + other = record.association(node.reflection.name) + + if node.reflection.collection? other.target.push(model) - other.set_inverse_instance(model) else - return record.association(join_part.reflection.name).target if record.association_cache.key?(join_part.reflection.name) - model = join_part.instantiate(row) - other = record.association(join_part.reflection.name) other.target = model - other.set_inverse_instance(model) end + + other.set_inverse_instance(model) model end end -- cgit v1.2.3