Skip to content

Commit 39997a0

Browse files
committed
Remove deprecated support to ActiveRecord::Base#read_attribute(:id) to return the custom primary key value
1 parent f40a697 commit 39997a0

File tree

4 files changed

+10
-19
lines changed

4 files changed

+10
-19
lines changed

activerecord/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Remove deprecated support to `ActiveRecord::Base#read_attribute(:id)` to return the custom primary key value.
2+
3+
*Rafael Mendonça França*
4+
15
* Remove deprecated `TestFixtures.fixture_path`.
26

37
*Rafael Mendonça França*

activerecord/lib/active_record/attribute_methods/read.rb

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,7 @@ def read_attribute(attr_name, &block)
3030
name = attr_name.to_s
3131
name = self.class.attribute_aliases[name] || name
3232

33-
return @attributes.fetch_value(name, &block) unless name == "id" && @primary_key
34-
35-
if self.class.composite_primary_key?
36-
@attributes.fetch_value("id", &block)
37-
else
38-
if @primary_key != "id"
39-
ActiveRecord.deprecator.warn(<<-MSG.squish)
40-
Using read_attribute(:id) to read the primary key value is deprecated.
41-
Use #id instead.
42-
MSG
43-
end
44-
@attributes.fetch_value(@primary_key, &block)
45-
end
33+
@attributes.fetch_value(name, &block)
4634
end
4735

4836
# This method exists to avoid the expensive primary_key check internally, without

activerecord/test/cases/primary_keys_test.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,11 @@ def test_read_attribute_id
4545
assert_equal 1, id
4646
end
4747

48-
def test_read_attribute_with_custom_primary_key
48+
def test_read_attribute_with_custom_primary_key_does_not_return_it_when_reading_the_id_attribute
4949
keyboard = Keyboard.create!
50-
msg = "Using read_attribute(:id) to read the primary key value is deprecated. Use #id instead."
51-
id = assert_deprecated(msg, ActiveRecord.deprecator) do
52-
keyboard.read_attribute(:id)
53-
end
50+
id = keyboard.read_attribute(:id)
5451

55-
assert_equal keyboard.key_number, id
52+
assert_nil id
5653
end
5754

5855
def test_read_attribute_with_composite_primary_key

guides/source/7_2_release_notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ Please refer to the [Changelog][active-record] for detailed changes.
138138

139139
* Remove deprecated `TestFixtures.fixture_path`.
140140

141+
* Remove deprecated support to `ActiveRecord::Base#read_attribute(:id)` to return the custom primary key value.
142+
141143
### Deprecations
142144

143145
* Deprecate `Rails.application.config.active_record.allow_deprecated_singular_associations_name`

0 commit comments

Comments
 (0)