aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/cases/persistence_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activerecord/test/cases/persistence_test.rb')
-rw-r--r--activerecord/test/cases/persistence_test.rb38
1 files changed, 36 insertions, 2 deletions
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index 77a5458b2a..1f6c224a11 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -471,6 +471,17 @@ class PersistencesTest < ActiveRecord::TestCase
assert_equal "Sebastian Topic", topic.title
end
+ def test_update_columns_should_not_use_setter_method
+ dev = Developer.find(1)
+ dev.instance_eval { def salary=(value); write_attribute(:salary, value * 2); end }
+
+ dev.update_columns(salary: 80000)
+ assert_equal 80000, dev.salary
+
+ dev.reload
+ assert_equal 80000, dev.salary
+ end
+
def test_update_columns_should_raise_exception_if_new_record
topic = Topic.new
assert_raises(ActiveRecord::ActiveRecordError) { topic.update_columns({ approved: false }) }
@@ -489,6 +500,14 @@ class PersistencesTest < ActiveRecord::TestCase
assert_equal [], topic.changed
end
+ def test_update_columns_with_model_having_primary_key_other_than_id
+ minivan = Minivan.find('m1')
+ new_name = 'sebavan'
+
+ minivan.update_columns(name: new_name)
+ assert_equal new_name, minivan.name
+ end
+
def test_update_columns_with_one_readonly_attribute
minivan = Minivan.find('m1')
prev_color = minivan.color
@@ -506,10 +525,10 @@ class PersistencesTest < ActiveRecord::TestCase
developer = Developer.find(1)
prev_month = Time.now.prev_month
- developer.update_column(:updated_at, prev_month)
+ developer.update_columns(updated_at: prev_month)
assert_equal prev_month, developer.updated_at
- developer.update_columns({ salary: 80000 })
+ developer.update_columns(salary: 80000)
assert_equal prev_month, developer.updated_at
assert_equal 80000, developer.salary
@@ -518,6 +537,21 @@ class PersistencesTest < ActiveRecord::TestCase
assert_equal 80000, developer.salary
end
+ def test_update_columns_with_one_changed_and_one_updated
+ t = Topic.order('id').limit(1).first
+ author_name = t.author_name
+ t.author_name = 'John'
+ t.update_columns(title: 'super_title')
+ assert_equal 'John', t.author_name
+ assert_equal 'super_title', t.title
+ assert t.changed?, "topic should have changed"
+ assert t.author_name_changed?, "author_name should have changed"
+
+ t.reload
+ assert_equal author_name, t.author_name
+ assert_equal 'super_title', t.title
+ end
+
def test_update_attributes
topic = Topic.find(1)
assert !topic.approved?