aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSantiago Pastorino <santiago@wyeworks.com>2015-01-18 21:19:00 -0200
committerSantiago Pastorino <santiago@wyeworks.com>2015-01-18 21:19:00 -0200
commite3f5be1831c45dfadbb748d21b45d3167222300a (patch)
tree79a6db248d9362d5be895b2c396c68dd79b2e2b9 /activerecord
parent9caeded9c62bf2c462a60a1a0b1a58249d4bd6ac (diff)
parent0fcd4cf5c26c470623eef9af72a134ef6ba1a701 (diff)
downloadrails-e3f5be1831c45dfadbb748d21b45d3167222300a.tar.gz
rails-e3f5be1831c45dfadbb748d21b45d3167222300a.tar.bz2
rails-e3f5be1831c45dfadbb748d21b45d3167222300a.zip
Merge pull request #18501 from prathamesh-sonpatki/nosql
Run SQL only if attribute changed for update_attribute method
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md4
-rw-r--r--activerecord/lib/active_record/persistence.rb2
-rw-r--r--activerecord/test/cases/persistence_test.rb10
3 files changed, 15 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index bfd3073293..d8f29a77b4 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,3 +1,7 @@
+* Don't run SQL if attribute value is not changed for update_attribute method.
+
+ *Prathamesh Sonpatki*
+
* `time` columns can now affected by `time_zone_aware_attributes`. If you have
set `config.time_zone` to a value other than `'UTC'`, they will be treated
as in that time zone by default in Rails 5.1. If this is not the desired
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb
index 6306a25745..714d36e7c0 100644
--- a/activerecord/lib/active_record/persistence.rb
+++ b/activerecord/lib/active_record/persistence.rb
@@ -246,7 +246,7 @@ module ActiveRecord
name = name.to_s
verify_readonly_attribute(name)
send("#{name}=", value)
- save(validate: false)
+ save(validate: false) if changed?
end
# Updates the attributes of the model from the passed-in hash and saves the
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb
index d6816041bc..2803ad2de0 100644
--- a/activerecord/test/cases/persistence_test.rb
+++ b/activerecord/test/cases/persistence_test.rb
@@ -356,6 +356,16 @@ class PersistenceTest < ActiveRecord::TestCase
assert_equal("David", topic_reloaded.author_name)
end
+ def test_update_attribute_does_not_run_sql_if_attribute_is_not_changed
+ klass = Class.new(Topic) do
+ def self.name; 'Topic'; end
+ end
+ topic = klass.create(title: 'Another New Topic')
+ assert_queries(0) do
+ topic.update_attribute(:title, 'Another New Topic')
+ end
+ end
+
def test_delete
topic = Topic.find(1)
assert_equal topic, topic.delete, 'topic.delete did not return self'