From a3c3cfdd0ebba26bb9dfc0bfd4e23a5f336730c0 Mon Sep 17 00:00:00 2001 From: Carlos Antonio da Silva Date: Thu, 6 Dec 2012 23:09:11 -0200 Subject: 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 --- activerecord/lib/active_record/persistence.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index fd3380a53c..36b6b5fce2 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -194,7 +194,7 @@ module ActiveRecord raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name) raise ActiveRecordError, "can not update on a new record object" unless persisted? - updated_count = self.class.update_all({ name => value }, self.class.primary_key => id) + updated_count = self.class.unscoped.update_all({ name => value }, self.class.primary_key => id) raw_write_attribute(name, value) -- cgit v1.2.3