Skip to content

Commit 5645149

Browse files
elebowjeremy
authored andcommitted
Deprecate update_attributes and update_attributes!
Closes #31998
1 parent 56278a7 commit 5645149

17 files changed

Lines changed: 44 additions & 62 deletions

activerecord/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
## Rails 6.0.0.alpha (Unreleased) ##
2+
* Deprecate `update_attributes`/`!` in favor of `update`/`!`.
3+
4+
*Eddie Lebow*
25

36
* Add ActiveRecord::Base.create_or_find_by/! to deal with the SELECT/INSERT race condition in
47
ActiveRecord::Base.find_or_create_by/! by leaning on unique constraints in the database.

activerecord/lib/active_record/internal_metadata.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def table_name
1717
end
1818

1919
def []=(key, value)
20-
find_or_initialize_by(key: key).update_attributes!(value: value)
20+
find_or_initialize_by(key: key).update!(value: value)
2121
end
2222

2323
def [](key)

activerecord/lib/active_record/persistence.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ def update(attributes)
418418
end
419419

420420
alias update_attributes update
421+
deprecate :update_attributes
421422

422423
# Updates its receiver just like #update but calls #save! instead
423424
# of +save+, so an exception is raised if the record is invalid and saving will fail.
@@ -431,6 +432,7 @@ def update!(attributes)
431432
end
432433

433434
alias update_attributes! update!
435+
deprecate :update_attributes!
434436

435437
# Equivalent to <code>update_columns(name => value)</code>.
436438
def update_column(name, value)

activerecord/test/cases/adapter_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def test_update_prepared_statement
2020
b = Book.create(name: "my \x00 book")
2121
b.reload
2222
assert_equal "my \x00 book", b.name
23-
b.update_attributes(name: "my other \x00 book")
23+
b.update(name: "my other \x00 book")
2424
b.reload
2525
assert_equal "my other \x00 book", b.name
2626
end

activerecord/test/cases/adapters/mysql2/transaction_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ class Sample < ActiveRecord::Base
4646
Sample.transaction do
4747
s1.lock!
4848
barrier.wait
49-
s2.update_attributes value: 1
49+
s2.update value: 1
5050
end
5151
end
5252

5353
begin
5454
Sample.transaction do
5555
s2.lock!
5656
barrier.wait
57-
s1.update_attributes value: 2
57+
s1.update value: 2
5858
end
5959
ensure
6060
thread.join

activerecord/test/cases/adapters/postgresql/transaction_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,15 @@ class Sample < ActiveRecord::Base
7676
Sample.transaction do
7777
s1.lock!
7878
barrier.wait
79-
s2.update_attributes value: 1
79+
s2.update value: 1
8080
end
8181
end
8282

8383
begin
8484
Sample.transaction do
8585
s2.lock!
8686
barrier.wait
87-
s1.update_attributes value: 2
87+
s1.update value: 2
8888
end
8989
ensure
9090
thread.join

activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ def test_symbol_join_table
662662
assert_includes developer.sym_special_projects, sp
663663
end
664664

665-
def test_update_attributes_after_push_without_duplicate_join_table_rows
665+
def test_update_columns_after_push_without_duplicate_join_table_rows
666666
developer = Developer.new("name" => "Kano")
667667
project = SpecialProject.create("name" => "Special Project")
668668
assert developer.save

activerecord/test/cases/associations/has_many_associations_test.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1211,20 +1211,20 @@ def test_custom_named_counter_cache
12111211
end
12121212
end
12131213

1214-
def test_calling_update_attributes_on_id_changes_the_counter_cache
1214+
def test_calling_update_on_id_changes_the_counter_cache
12151215
topic = Topic.order("id ASC").first
12161216
original_count = topic.replies.to_a.size
12171217
assert_equal original_count, topic.replies_count
12181218

12191219
first_reply = topic.replies.first
1220-
first_reply.update_attributes(parent_id: nil)
1220+
first_reply.update(parent_id: nil)
12211221
assert_equal original_count - 1, topic.reload.replies_count
12221222

1223-
first_reply.update_attributes(parent_id: topic.id)
1223+
first_reply.update(parent_id: topic.id)
12241224
assert_equal original_count, topic.reload.replies_count
12251225
end
12261226

1227-
def test_calling_update_attributes_changing_ids_doesnt_change_counter_cache
1227+
def test_calling_update_changing_ids_doesnt_change_counter_cache
12281228
topic1 = Topic.find(1)
12291229
topic2 = Topic.find(3)
12301230
original_count1 = topic1.replies.to_a.size
@@ -1233,11 +1233,11 @@ def test_calling_update_attributes_changing_ids_doesnt_change_counter_cache
12331233
reply1 = topic1.replies.first
12341234
reply2 = topic2.replies.first
12351235

1236-
reply1.update_attributes(parent_id: topic2.id)
1236+
reply1.update(parent_id: topic2.id)
12371237
assert_equal original_count1 - 1, topic1.reload.replies_count
12381238
assert_equal original_count2 + 1, topic2.reload.replies_count
12391239

1240-
reply2.update_attributes(parent_id: topic1.id)
1240+
reply2.update(parent_id: topic1.id)
12411241
assert_equal original_count1, topic1.reload.replies_count
12421242
assert_equal original_count2, topic2.reload.replies_count
12431243
end

activerecord/test/cases/autosave_association_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def self.name; "Reference"; end
5050
}
5151

5252
u = person.create!(first_name: "cool")
53-
u.update_attributes!(first_name: "nah") # still valid because validation only applies on 'create'
53+
u.update!(first_name: "nah") # still valid because validation only applies on 'create'
5454
assert_predicate reference.create!(person: u), :persisted?
5555
end
5656

activerecord/test/cases/batches_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ def test_in_batches_relations_with_condition_should_not_overlap_with_each_other
508508
def test_in_batches_relations_update_all_should_not_affect_matching_records_in_other_batches
509509
Post.update_all(author_id: 0)
510510
person = Post.last
511-
person.update_attributes(author_id: 1)
511+
person.update(author_id: 1)
512512

513513
Post.in_batches(of: 2) do |batch|
514514
batch.where("author_id >= 1").update_all("author_id = author_id + 1")

0 commit comments

Comments
 (0)