diff options
author | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-12-06 23:09:11 -0200 |
---|---|---|
committer | Carlos Antonio da Silva <carlosantoniodasilva@gmail.com> | 2012-12-06 23:49:53 -0200 |
commit | a3c3cfdd0ebba26bb9dfc0bfd4e23a5f336730c0 (patch) | |
tree | c2a9ce17e80fab8246b005aced974a4d45907d29 /activerecord/test/cases/persistence_test.rb | |
parent | d70539c1ec4c1f615bb359468b7b0c58276bf71b (diff) | |
download | rails-a3c3cfdd0ebba26bb9dfc0bfd4e23a5f336730c0.tar.gz rails-a3c3cfdd0ebba26bb9dfc0bfd4e23a5f336730c0.tar.bz2 rails-a3c3cfdd0ebba26bb9dfc0bfd4e23a5f336730c0.zip |
Unscope update_column(s) query to ignore default scope
When applying default_scope to a class with a where clause, using
update_column(s) could generate a query that would not properly update
the record due to the where clause from the default_scope being applied
to the update query.
class User < ActiveRecord::Base
default_scope where(active: true)
end
user = User.first
user.active = false
user.save!
user.update_column(:active, true) # => false
In this situation we want to skip the default_scope clause and just
update the record based on the primary key. With this change:
user.update_column(:active, true) # => true
Backport of #8436 fix.
Conflicts:
activerecord/CHANGELOG.md
activerecord/lib/active_record/persistence.rb
activerecord/test/cases/persistence_test.rb
Diffstat (limited to 'activerecord/test/cases/persistence_test.rb')
-rw-r--r-- | activerecord/test/cases/persistence_test.rb | 8 |
1 files changed, 8 insertions, 0 deletions
diff --git a/activerecord/test/cases/persistence_test.rb b/activerecord/test/cases/persistence_test.rb index a35cb3c4a5..00dc1f6d72 100644 --- a/activerecord/test/cases/persistence_test.rb +++ b/activerecord/test/cases/persistence_test.rb @@ -519,6 +519,14 @@ class PersistencesTest < ActiveRecord::TestCase assert return_value end + def test_update_column_with_default_scope + developer = DeveloperCalledDavid.first + developer.name = 'John' + developer.save! + + assert developer.update_column(:name, 'Will'), 'did not update record due to default scope' + end + def test_update_attributes topic = Topic.find(1) assert !topic.approved? |