aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2013-10-24 22:37:29 -0200
committerRafael Mendonça França <rafaelmfranca@gmail.com>2013-10-24 22:37:29 -0200
commit7160ffbe59ef99f01f530ad6e8377f8a91218396 (patch)
tree83da2b43c2d3bf7a6ed329f33b420f75e132c69d /activerecord
parenta53f7167061a2128ed256593845a456f4c717dfe (diff)
parent8022fc4913d5fa285889795617a1f37c5aa705a9 (diff)
downloadrails-7160ffbe59ef99f01f530ad6e8377f8a91218396.tar.gz
rails-7160ffbe59ef99f01f530ad6e8377f8a91218396.tar.bz2
rails-7160ffbe59ef99f01f530ad6e8377f8a91218396.zip
Merge pull request #12621 from laurocaetano/fix_has_one_association_with_primary_key_set
Save association when primary key is manually set Conflicts: activerecord/CHANGELOG.md
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md6
-rw-r--r--activerecord/lib/active_record/autosave_association.rb8
-rw-r--r--activerecord/test/cases/associations/has_one_associations_test.rb11
3 files changed, 24 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index e93a26d7c5..25b905891f 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
+* Save `has_one` association when primary key is manually set.
+
+ Fixes #12302.
+
+ *Lauro Caetano*
+
* Allow any version of BCrypt when using `has_secury_password`.
*Mike Perham*
diff --git a/activerecord/lib/active_record/autosave_association.rb b/activerecord/lib/active_record/autosave_association.rb
index 561b2dd6d1..e9622ca0c1 100644
--- a/activerecord/lib/active_record/autosave_association.rb
+++ b/activerecord/lib/active_record/autosave_association.rb
@@ -384,7 +384,8 @@ module ActiveRecord
record.destroy
else
key = reflection.options[:primary_key] ? send(reflection.options[:primary_key]) : id
- if autosave != false && (new_record? || record.new_record? || record[reflection.foreign_key] != key || autosave)
+ if autosave != false && (autosave || new_record? || record_changed?(reflection, record, key))
+
unless reflection.through_reflection
record[reflection.foreign_key] = key
end
@@ -397,6 +398,11 @@ module ActiveRecord
end
end
+ # If the record is new or it has changed, returns true.
+ def record_changed?(reflection, record, key)
+ record.new_record? || record[reflection.foreign_key] != key || record.attribute_changed?(reflection.foreign_key)
+ end
+
# Saves the associated record if it's new or <tt>:autosave</tt> is enabled.
#
# In addition, it will destroy the association if it was marked for destruction.
diff --git a/activerecord/test/cases/associations/has_one_associations_test.rb b/activerecord/test/cases/associations/has_one_associations_test.rb
index 9cd4db8dc9..cdd386187b 100644
--- a/activerecord/test/cases/associations/has_one_associations_test.rb
+++ b/activerecord/test/cases/associations/has_one_associations_test.rb
@@ -524,4 +524,15 @@ class HasOneAssociationsTest < ActiveRecord::TestCase
assert_equal 'new name', pirate.ship.reload.name
end
+ def test_has_one_autosave_with_primary_key_manually_set
+ post = Post.create(id: 1234, title: "Some title", body: 'Some content')
+ author = Author.new(id: 33, name: 'Hank Moody')
+
+ author.post = post
+ author.save
+ author.reload
+
+ assert_not_nil author.post
+ assert_equal author.post, post
+ end
end