Skip to content

Commit

Permalink
Do not use update_column where update_attribute is not interchangeable
Browse files Browse the repository at this point in the history
Revert "Deprecate update_attribute."

This reverts commit b081f6b.

Reason: Since the new deprecation policy we removed the deprecation of
update_attribute but we didn't reverted the changes to use
update_column.

Fixes #7306
  • Loading branch information
rafaelfranca committed Aug 15, 2012
1 parent 786713a commit 8055cd6
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 39 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.9 (unreleased)

* Fix `increment!`, `decrement!`, `toggle!` that was skipping callbacks.
Fixes #7306.

*Rafael Mendonça França*

* Fix AR#create to return an unsaved record when AR::RecordInvalid is
raised. Fixes #3217.

Expand Down
Expand Up @@ -38,7 +38,7 @@ def delete(method = options[:dependent])
when :destroy
target.destroy
when :nullify
target.update_column(reflection.foreign_key, nil)
target.update_attribute(reflection.foreign_key, nil)
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -232,7 +232,7 @@ def initialize(name)
# add_column :people, :salary, :integer
# Person.reset_column_information
# Person.all.each do |p|
# p.update_column :salary, SalaryCalculator.compute(p)
# p.update_attribute :salary, SalaryCalculator.compute(p)
# end
# end
# end
Expand All @@ -252,7 +252,7 @@ def initialize(name)
# ...
# say_with_time "Updating salaries..." do
# Person.all.each do |p|
# p.update_column :salary, SalaryCalculator.compute(p)
# p.update_attribute :salary, SalaryCalculator.compute(p)
# end
# end
# ...
Expand Down
9 changes: 3 additions & 6 deletions activerecord/lib/active_record/persistence.rb
Expand Up @@ -174,9 +174,6 @@ def becomes(klass)
# * updated_at/updated_on column is updated if that column is available.
# * Updates all the attributes that are dirty in this object.
#
# This method has been deprecated in favor of <tt>update_column</tt> due to
# its similarity with <tt>update_attributes</tt>.
#
def update_attribute(name, value)
name = name.to_s
raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
Expand Down Expand Up @@ -242,7 +239,7 @@ def increment(attribute, by = 1)
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def increment!(attribute, by = 1)
increment(attribute, by).update_column(attribute, self[attribute])
increment(attribute, by).update_attribute(attribute, self[attribute])
end

# Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
Expand All @@ -259,7 +256,7 @@ def decrement(attribute, by = 1)
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def decrement!(attribute, by = 1)
decrement(attribute, by).update_column(attribute, self[attribute])
decrement(attribute, by).update_attribute(attribute, self[attribute])
end

# Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
Expand All @@ -276,7 +273,7 @@ def toggle(attribute)
# Saving is not subjected to validation checks. Returns +true+ if the
# record could be saved.
def toggle!(attribute)
toggle(attribute).update_column(attribute, self[attribute])
toggle(attribute).update_attribute(attribute, self[attribute])
end

# Reloads the attributes of this object from the database.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/cases/base_test.rb
Expand Up @@ -2133,7 +2133,7 @@ def test_cache_key_format_for_existing_record_with_updated_at

def test_cache_key_format_for_existing_record_with_nil_updated_at
dev = Developer.first
dev.update_column(:updated_at, nil)
dev.update_attribute(:updated_at, nil)
assert_match(/\/#{dev.id}$/, dev.cache_key)
end

Expand Down
10 changes: 10 additions & 0 deletions activerecord/test/cases/dirty_test.rb
Expand Up @@ -509,6 +509,16 @@ def test_previous_changes
assert_not_nil pirate.previous_changes['updated_on'][1]
assert !pirate.previous_changes.key?('parrot_id')
assert !pirate.previous_changes.key?('created_on')

pirate = Pirate.find_by_catchphrase("Ahoy!")
pirate.update_attribute(:catchphrase, "Ninjas suck!")

assert_equal 2, pirate.previous_changes.size
assert_equal ["Ahoy!", "Ninjas suck!"], pirate.previous_changes['catchphrase']
assert_not_nil pirate.previous_changes['updated_on'][0]
assert_not_nil pirate.previous_changes['updated_on'][1]
assert !pirate.previous_changes.key?('parrot_id')
assert !pirate.previous_changes.key?('created_on')
end

private
Expand Down
35 changes: 8 additions & 27 deletions activerecord/test/cases/persistence_test.rb
Expand Up @@ -355,17 +355,10 @@ def test_destroy_record_with_associations

def test_update_attribute
assert !Topic.find(1).approved?

ActiveSupport::Deprecation.silence do
Topic.find(1).update_attribute("approved", true)
end

Topic.find(1).update_attribute("approved", true)
assert Topic.find(1).approved?

ActiveSupport::Deprecation.silence do
Topic.find(1).update_attribute(:approved, false)
end

Topic.find(1).update_attribute(:approved, false)
assert !Topic.find(1).approved?
end

Expand All @@ -375,10 +368,7 @@ def test_update_attribute_does_not_choke_on_nil

def test_update_attribute_for_readonly_attribute
minivan = Minivan.find('m1')

ActiveSupport::Deprecation.silence do
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
end
assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
end

# This test is correct, but it is hard to fix it since
Expand All @@ -404,11 +394,8 @@ def test_update_attribute_for_readonly_attribute

def test_update_attribute_with_one_updated
t = Topic.first

ActiveSupport::Deprecation.silence do
t.update_attribute(:title, 'super_title')
end

title = t.title
t.update_attribute(:title, 'super_title')
assert_equal 'super_title', t.title
assert !t.changed?, "topic should not have changed"
assert !t.title_changed?, "title should not have changed"
Expand All @@ -422,16 +409,10 @@ def test_update_attribute_for_updated_at_on
developer = Developer.find(1)
prev_month = Time.now.prev_month

ActiveSupport::Deprecation.silence do
developer.update_attribute(:updated_at, prev_month)
end

developer.update_attribute(:updated_at, prev_month)
assert_equal prev_month, developer.updated_at

ActiveSupport::Deprecation.silence do
developer.update_attribute(:salary, 80001)
end

developer.update_attribute(:salary, 80001)
assert_not_equal prev_month, developer.updated_at

developer.reload
Expand Down Expand Up @@ -469,7 +450,7 @@ def test_update_column_should_raise_exception_if_new_record

def test_update_column_should_not_leave_the_object_dirty
topic = Topic.find(1)
topic.update_column("content", "Have a nice day")
topic.update_attribute("content", "Have a nice day")

topic.reload
topic.update_column(:content, "You too")
Expand Down
4 changes: 2 additions & 2 deletions activerecord/test/cases/timestamp_test.rb
Expand Up @@ -11,7 +11,7 @@ class TimestampTest < ActiveRecord::TestCase

def setup
@developer = Developer.order(:id).first
@developer.update_column(:updated_at, Time.now.prev_month)
@developer.update_attribute(:updated_at, Time.now.prev_month)
@previously_updated_at = @developer.updated_at
end

Expand Down Expand Up @@ -133,7 +133,7 @@ def test_touching_a_record_with_a_belongs_to_that_uses_a_counter_cache_should_up

pet = Pet.first
owner = pet.owner
owner.update_column(:happy_at, 3.days.ago)
owner.update_attribute(:happy_at, 3.days.ago)
previously_owner_updated_at = owner.updated_at

pet.name = "I'm a parrot"
Expand Down

0 comments on commit 8055cd6

Please sign in to comment.