Skip to content

Commit 2bdee49

Browse files
committed
Don't re-assign unchanged belongs_to associations during saves
1 parent 06a872f commit 2bdee49

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

activerecord/lib/active_record/autosave_association.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def save_belongs_to_association(reflection)
501501

502502
if association.updated?
503503
association_id = record.public_send(reflection.options[:primary_key] || :id)
504-
self[reflection.foreign_key] = association_id
504+
self[reflection.foreign_key] = association_id unless self[reflection.foreign_key] == association_id
505505
association.loaded!
506506
end
507507

activerecord/test/cases/base_test.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ class NonRaisingPost < Post
8484

8585
ActiveRecord.raise_on_assign_to_attr_readonly = previous_value
8686

87+
class ReadonlyAuthorPost < Post
88+
attr_readonly :author_id
89+
end
90+
8791
class Weird < ActiveRecord::Base; end
8892

8993
class LintTest < ActiveRecord::TestCase
@@ -844,6 +848,33 @@ def test_readonly_attributes_when_configured_to_not_raise
844848
assert_equal "changed via []=", post.body
845849
end
846850

851+
def test_readonly_attributes_on_belongs_to_association
852+
assert_equal [ "author_id" ], ReadonlyAuthorPost.readonly_attributes
853+
854+
author1 = Author.create!(name: "Alex")
855+
author2 = Author.create!(name: "Not Alex")
856+
857+
post_with_reload = ReadonlyAuthorPost.create!(author: author1, title: "Hi", body: "there")
858+
post_with_reload.reload
859+
post_with_reload.update(title: "Hello", body: "world")
860+
assert_equal author1, post_with_reload.author
861+
862+
post_with_reload2 = ReadonlyAuthorPost.create!(author: author1, title: "Hi", body: "there")
863+
post_with_reload2.reload
864+
assert_raises(ActiveRecord::ReadonlyAttributeError) do
865+
post_with_reload2.update(author: author2)
866+
end
867+
868+
post_without_reload = ReadonlyAuthorPost.create!(author: author1, title: "Hi", body: "there")
869+
post_without_reload.update(title: "Hello", body: "world")
870+
assert_equal author1, post_without_reload.author
871+
872+
post_without_reload2 = ReadonlyAuthorPost.create!(author: author1, title: "Hi", body: "there")
873+
assert_raises(ActiveRecord::ReadonlyAttributeError) do
874+
post_without_reload2.update(author: author2)
875+
end
876+
end
877+
847878
def test_unicode_column_name
848879
Weird.reset_column_information
849880
weird = Weird.create(なまえ: "たこ焼き仮面")

0 commit comments

Comments
 (0)