Skip to content
This repository

Deprecate update_attribute #6739

Merged
merged 1 commit into from almost 2 years ago

2 participants

Steve Klabnik Xavier Noria
Steve Klabnik
Collaborator

Historically, update_attribute and update_attributes are similar, but
with one big difference: update_attribute does not run validations.
These two methods are really easy to confuse given their similar
names. Therefore, update_attribute is being deprecated in favor of
update_column, and will be removed in Rails 4.

See the discussion on rails-core here:
https://groups.google.com/d/topic/rubyonrails-core/BWPUTK7WvYA/discussion

Xavier Noria
Owner
fxn commented June 14, 2012

I think the deprecation message should warn a little about the differences. Something like "use update_column if you do not need mass-assignment protection, callbacks, or touching updated_at, otherwise switch to update_attributes"... something in that line.

Steve Klabnik Deprecate update_attribute.
Historically, update_attribute and update_attributes are similar, but
with one big difference: update_attribute does not run validations.
These two methods are really easy to confuse given their similar
names. Therefore, update_attribute is being deprecated in favor of
update_column, and will be removed in Rails 4.

See the discussion on rails-core here:
https://groups.google.com/d/topic/rubyonrails-core/BWPUTK7WvYA/discussion
b081f6b
Steve Klabnik
Collaborator

Added in a better message, also silenced warnings from the tests.

Xavier Noria fxn merged commit 63bcfbb into from June 14, 2012
Xavier Noria fxn closed this June 14, 2012
Rainer Jung rynr referenced this pull request in geekq/workflow December 24, 2012
Closed

Use of update_attributes in recent commit #57

Brendon Murphy bemurphy referenced this pull request from a commit February 12, 2013
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Jun 14, 2012
Steve Klabnik Deprecate update_attribute.
Historically, update_attribute and update_attributes are similar, but
with one big difference: update_attribute does not run validations.
These two methods are really easy to confuse given their similar
names. Therefore, update_attribute is being deprecated in favor of
update_column, and will be removed in Rails 4.

See the discussion on rails-core here:
https://groups.google.com/d/topic/rubyonrails-core/BWPUTK7WvYA/discussion
b081f6b
This page is out of date. Refresh to see the latest.
2  activerecord/lib/active_record/associations/has_one_association.rb
@@ -36,7 +36,7 @@ def delete(method = options[:dependent])
36 36
             when :destroy
37 37
               target.destroy
38 38
             when :nullify
39  
-              target.update_attribute(reflection.foreign_key, nil)
  39
+              target.update_column(reflection.foreign_key, nil)
40 40
           end
41 41
         end
42 42
       end
4  activerecord/lib/active_record/migration.rb
@@ -232,7 +232,7 @@ def initialize(name)
232 232
   #       add_column :people, :salary, :integer
233 233
   #       Person.reset_column_information
234 234
   #       Person.all.each do |p|
235  
-  #         p.update_attribute :salary, SalaryCalculator.compute(p)
  235
+  #         p.update_column :salary, SalaryCalculator.compute(p)
236 236
   #       end
237 237
   #     end
238 238
   #   end
@@ -252,7 +252,7 @@ def initialize(name)
252 252
   #     ...
253 253
   #     say_with_time "Updating salaries..." do
254 254
   #       Person.all.each do |p|
255  
-  #         p.update_attribute :salary, SalaryCalculator.compute(p)
  255
+  #         p.update_column :salary, SalaryCalculator.compute(p)
256 256
   #       end
257 257
   #     end
258 258
   #     ...
10  activerecord/lib/active_record/persistence.rb
@@ -174,8 +174,12 @@ def becomes(klass)
174 174
     # * updated_at/updated_on column is updated if that column is available.
175 175
     # * Updates all the attributes that are dirty in this object.
176 176
     #
  177
+    # This method has been deprecated in favor of <tt>update_column</tt> due to
  178
+    # its similarity with <tt>update_attributes</tt>.
  179
+    #
177 180
     def update_attribute(name, value)
178 181
       name = name.to_s
  182
+      ActiveSupport::Deprecation.warn("update_attribute is deprecated and will be removed in Rails 4. If you want to skip mass-assignment protection, callbacks, and modifying updated_at, use update_column. If you do want those things, use update_attributes.")
179 183
       raise ActiveRecordError, "#{name} is marked as readonly" if self.class.readonly_attributes.include?(name)
180 184
       send("#{name}=", value)
181 185
       save(:validate => false)
@@ -239,7 +243,7 @@ def increment(attribute, by = 1)
239 243
     # Saving is not subjected to validation checks. Returns +true+ if the
240 244
     # record could be saved.
241 245
     def increment!(attribute, by = 1)
242  
-      increment(attribute, by).update_attribute(attribute, self[attribute])
  246
+      increment(attribute, by).update_column(attribute, self[attribute])
243 247
     end
244 248
 
245 249
     # Initializes +attribute+ to zero if +nil+ and subtracts the value passed as +by+ (default is 1).
@@ -256,7 +260,7 @@ def decrement(attribute, by = 1)
256 260
     # Saving is not subjected to validation checks. Returns +true+ if the
257 261
     # record could be saved.
258 262
     def decrement!(attribute, by = 1)
259  
-      decrement(attribute, by).update_attribute(attribute, self[attribute])
  263
+      decrement(attribute, by).update_column(attribute, self[attribute])
260 264
     end
261 265
 
262 266
     # Assigns to +attribute+ the boolean opposite of <tt>attribute?</tt>. So
@@ -273,7 +277,7 @@ def toggle(attribute)
273 277
     # Saving is not subjected to validation checks. Returns +true+ if the
274 278
     # record could be saved.
275 279
     def toggle!(attribute)
276  
-      toggle(attribute).update_attribute(attribute, self[attribute])
  280
+      toggle(attribute).update_column(attribute, self[attribute])
277 281
     end
278 282
 
279 283
     # Reloads the attributes of this object from the database.
2  activerecord/test/cases/base_test.rb
@@ -2127,7 +2127,7 @@ def test_cache_key_format_for_existing_record_with_updated_at
2127 2127
 
2128 2128
   def test_cache_key_format_for_existing_record_with_nil_updated_at
2129 2129
     dev = Developer.first
2130  
-    dev.update_attribute(:updated_at, nil)
  2130
+    dev.update_column(:updated_at, nil)
2131 2131
     assert_match(/\/#{dev.id}$/, dev.cache_key)
2132 2132
   end
2133 2133
 
2  activerecord/test/cases/dirty_test.rb
@@ -498,7 +498,7 @@ def test_previous_changes
498 498
     assert !pirate.previous_changes.key?('created_on')
499 499
 
500 500
     pirate = Pirate.find_by_catchphrase("Ahoy!")
501  
-    pirate.update_attribute(:catchphrase, "Ninjas suck!")
  501
+    pirate.update_column(:catchphrase, "Ninjas suck!")
502 502
 
503 503
     assert_equal 2, pirate.previous_changes.size
504 504
     assert_equal ["Ahoy!", "Ninjas suck!"], pirate.previous_changes['catchphrase']
34  activerecord/test/cases/persistence_test.rb
@@ -355,10 +355,17 @@ def test_destroy_record_with_associations
355 355
 
356 356
   def test_update_attribute
357 357
     assert !Topic.find(1).approved?
358  
-    Topic.find(1).update_attribute("approved", true)
  358
+
  359
+    ActiveSupport::Deprecation.silence do
  360
+      Topic.find(1).update_attribute("approved", true)
  361
+    end
  362
+
359 363
     assert Topic.find(1).approved?
360 364
 
361  
-    Topic.find(1).update_attribute(:approved, false)
  365
+    ActiveSupport::Deprecation.silence do
  366
+      Topic.find(1).update_attribute(:approved, false)
  367
+    end
  368
+
362 369
     assert !Topic.find(1).approved?
363 370
   end
364 371
 
@@ -368,7 +375,10 @@ def test_update_attribute_does_not_choke_on_nil
368 375
 
369 376
   def test_update_attribute_for_readonly_attribute
370 377
     minivan = Minivan.find('m1')
371  
-    assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
  378
+
  379
+    ActiveSupport::Deprecation.silence do
  380
+      assert_raises(ActiveRecord::ActiveRecordError) { minivan.update_attribute(:color, 'black') }
  381
+    end
372 382
   end
373 383
 
374 384
   # This test is correct, but it is hard to fix it since
@@ -395,7 +405,11 @@ def test_update_attribute_for_readonly_attribute
395 405
   def test_update_attribute_with_one_updated
396 406
     t = Topic.first
397 407
     title = t.title
398  
-    t.update_attribute(:title, 'super_title')
  408
+
  409
+    ActiveSupport::Deprecation.silence do
  410
+      t.update_attribute(:title, 'super_title')
  411
+    end
  412
+
399 413
     assert_equal 'super_title', t.title
400 414
     assert !t.changed?, "topic should not have changed"
401 415
     assert !t.title_changed?, "title should not have changed"
@@ -409,10 +423,16 @@ def test_update_attribute_for_updated_at_on
409 423
     developer = Developer.find(1)
410 424
     prev_month = Time.now.prev_month
411 425
 
412  
-    developer.update_attribute(:updated_at, prev_month)
  426
+    ActiveSupport::Deprecation.silence do
  427
+      developer.update_attribute(:updated_at, prev_month)
  428
+    end
  429
+
413 430
     assert_equal prev_month, developer.updated_at
414 431
 
415  
-    developer.update_attribute(:salary, 80001)
  432
+    ActiveSupport::Deprecation.silence do
  433
+      developer.update_attribute(:salary, 80001)
  434
+    end
  435
+
416 436
     assert_not_equal prev_month, developer.updated_at
417 437
 
418 438
     developer.reload
@@ -450,7 +470,7 @@ def test_update_column_should_raise_exception_if_new_record
450 470
 
451 471
   def test_update_column_should_not_leave_the_object_dirty
452 472
     topic = Topic.find(1)
453  
-    topic.update_attribute("content", "Have a nice day")
  473
+    topic.update_column("content", "Have a nice day")
454 474
 
455 475
     topic.reload
456 476
     topic.update_column(:content, "You too")
4  activerecord/test/cases/timestamp_test.rb
@@ -11,7 +11,7 @@ class TimestampTest < ActiveRecord::TestCase
11 11
 
12 12
   def setup
13 13
     @developer = Developer.order(:id).first
14  
-    @developer.update_attribute(:updated_at, Time.now.prev_month)
  14
+    @developer.update_column(:updated_at, Time.now.prev_month)
15 15
     @previously_updated_at = @developer.updated_at
16 16
   end
17 17
 
@@ -133,7 +133,7 @@ def test_touching_a_record_with_a_belongs_to_that_uses_a_counter_cache_should_up
133 133
 
134 134
     pet = Pet.first
135 135
     owner = pet.owner
136  
-    owner.update_attribute(:happy_at, 3.days.ago)
  136
+    owner.update_column(:happy_at, 3.days.ago)
137 137
     previously_owner_updated_at = owner.updated_at
138 138
 
139 139
     pet.name = "I'm a parrot"
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.