Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Run SQL only if attribute changed for update_attribute method
Browse files Browse the repository at this point in the history
 - This is based on rails#18400 but
   tackling same issue with update_attribute method instead of update method.
  • Loading branch information
prathamesh-sonpatki committed Jan 18, 2015
1 parent 8a87ebf commit 0fcd4cf
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 4 additions & 0 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/persistence.rb
Expand Up @@ -246,7 +246,7 @@ def update_attribute(name, value)
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
Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/persistence_test.rb
Expand Up @@ -356,6 +356,16 @@ def self.name; 'Topic'; end
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'
Expand Down

0 comments on commit 0fcd4cf

Please sign in to comment.