Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

update_column can't update record which filter by default scope #8436

Closed
raykin opened this Issue · 1 comment

2 participants

@raykin

Suppose we have a User class

class User < ActiveRecord::Base                                                                                                          
  default_scope where(:active => true) 
end

and failed spec

it "update_column can change user active column" , :focus => true do                                                                 
  user.active.should be_false                                                                                                        
  user.update_column :active, true                                                                                                   
  user.reload.active.should be_true  # failed here , column active still got value false                                                                                      
end                                                                                                                                  

on source of update_column

    def update_column(name, value)
      name = name.to_s
      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?
      raw_write_attribute(name, value)
      self.class.update_all({ name => value }, self.class.primary_key => id) == 1
    end

if I edit last second line as

   self.class.unscoped.update_all({ name => value }, self.class.primary_key => id) == 1

then spec passed.

@carlosantoniodasilva

Can you send a pull request @raykin? Otherwise someone else can give it a try. Thanks!

@carlosantoniodasilva carlosantoniodasilva closed this issue from a commit
@carlosantoniodasilva carlosantoniodasilva 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

Fixes #8436.
0e67f79
@carlosantoniodasilva carlosantoniodasilva referenced this issue from a commit
@carlosantoniodasilva carlosantoniodasilva 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
a3c3cfd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.