From 948b931925febac3c965ab13470065ced68f7b53 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 26 Feb 2018 11:16:10 -0700 Subject: Never attempt to write virtual attributes to the database Currently the place where we limit what gets sent to the database is in the implementation for `partial_writes`. We should also be restricting it to column names when partial writes are turned off. Note that we're using `&` instead of just defaulting to `self.class.column_names`, as the instance version of `attribute_names` does not include attributes which are uninitialized (were not included in the select clause) --- activerecord/test/cases/dirty_test.rb | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'activerecord/test/cases/dirty_test.rb') diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 14c4e3cbd4..d1a7366f5f 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -736,6 +736,25 @@ class DirtyTest < ActiveRecord::TestCase assert record.save end + test "virtual attributes are not written with partial_writes off" do + original_partial_writes = ActiveRecord::Base.partial_writes + begin + ActiveRecord::Base.partial_writes = false + klass = Class.new(ActiveRecord::Base) do + self.table_name = "people" + attribute :non_persisted_attribute, :string + end + + record = klass.new(first_name: "Sean") + record.non_persisted_attribute_will_change! + + assert_predicate record, :non_persisted_attribute_changed? + assert record.save + ensure + ActiveRecord::Base.partial_writes = original_partial_writes + end + end + test "mutating and then assigning doesn't remove the change" do pirate = Pirate.create!(catchphrase: "arrrr") pirate.catchphrase << " matey!" -- cgit v1.2.3