aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-03-30 12:18:47 +0900
committerGitHub <noreply@github.com>2018-03-30 12:18:47 +0900
commit7231b59004a0165afcd5496f33b60354cd45e73e (patch)
tree94a98112801dbf2ca0a29008bcddf7b8d76e9a91 /activerecord
parenteb807384c81a6e086b17a576755e992e6c4c685e (diff)
parent0cdeda5826fc7cf26de404dcdd6d747531a32b6c (diff)
downloadrails-7231b59004a0165afcd5496f33b60354cd45e73e.tar.gz
rails-7231b59004a0165afcd5496f33b60354cd45e73e.tar.bz2
rails-7231b59004a0165afcd5496f33b60354cd45e73e.zip
Merge pull request #32338 from eugeneius/dont_clobber_foreign_key
Don't unset foreign key when preloading missing record
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/associations/preloader/association.rb4
-rw-r--r--activerecord/test/cases/associations/eager_test.rb2
2 files changed, 4 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb
index 0f38d6bbda..d6f7359055 100644
--- a/activerecord/lib/active_record/associations/preloader/association.rb
+++ b/activerecord/lib/active_record/associations/preloader/association.rb
@@ -42,11 +42,11 @@ module ActiveRecord
def associate_records_to_owner(owner, records)
association = owner.association(reflection.name)
+ association.loaded!
if reflection.collection?
- association.loaded!
association.target.concat(records)
else
- association.target = records.first
+ association.target = records.first unless records.empty?
end
end
diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb
index f18c6177ac..6e0cf30092 100644
--- a/activerecord/test/cases/associations/eager_test.rb
+++ b/activerecord/test/cases/associations/eager_test.rb
@@ -1218,6 +1218,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
client = assert_queries(2) { Client.preload(:firm).find(c.id) }
assert_no_queries { assert_nil client.firm }
+ assert_equal c.client_of, client.client_of
end
def test_preloading_empty_belongs_to_polymorphic
@@ -1225,6 +1226,7 @@ class EagerAssociationTest < ActiveRecord::TestCase
tagging = assert_queries(2) { Tagging.preload(:taggable).find(t.id) }
assert_no_queries { assert_nil tagging.taggable }
+ assert_equal t.taggable_id, tagging.taggable_id
end
def test_preloading_through_empty_belongs_to