Skip to content

Commit

Permalink
Allow object_id as a column name for ActiveRecord
Browse files Browse the repository at this point in the history
Fixes rails#50160

The `object_id` name may be used by polymorphic relations where `object` is the best name, like `notification.object`.
In that case two columns are created: `object_id` and `object_type`.

Update tests and comments to reference `__id__` instead of `object_id` for consistency.
  • Loading branch information
misdoro authored and byroot committed Nov 28, 2023
1 parent 82e33e4 commit 622c58c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/attribute_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def dangerous_attribute_methods # :nodoc:
Base.private_instance_methods -
Base.superclass.instance_methods -
Base.superclass.private_instance_methods +
%i[__id__ dup freeze frozen? hash object_id class clone]
%i[__id__ dup freeze frozen? hash class clone]
).map { |m| -m.to_s }.to_set.freeze
end
end
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/associations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1261,7 +1261,7 @@ def test_preload_with_available_records_with_through_association
end

assert_predicate author.association(:essay_category), :loaded?
assert categories.map(&:object_id).include?(author.essay_category.object_id)
assert categories.map(&:__id__).include?(author.essay_category.__id__)
end

def test_preload_with_only_some_records_available_with_through_associations
Expand Down Expand Up @@ -1307,7 +1307,7 @@ def test_preload_with_available_records_queries_when_scoped
end

assert_predicate post.association(:author), :loaded?
assert_not_equal david.object_id, post.author.object_id
assert_not_equal david.__id__, post.author.__id__
end

def test_preload_with_available_records_queries_when_collection
Expand All @@ -1319,7 +1319,7 @@ def test_preload_with_available_records_queries_when_collection
end

assert_predicate post.association(:comments), :loaded?
assert_empty post.comments.map(&:object_id) & comments.map(&:object_id)
assert_empty post.comments.map(&:__id__) & comments.map(&:__id__)
end

def test_preload_with_available_records_queries_when_incomplete
Expand Down

0 comments on commit 622c58c

Please sign in to comment.