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 committed Nov 24, 2023
1 parent 82e33e4 commit 1fca131
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ def association_instance_set(name, association)
#
# d = Dungeon.first
# t = d.traps.first
# d.object_id == t.dungeon.object_id # => true
# d.__id__ == t.dungeon.__id__ # => true
#
# The +Dungeon+ instances +d+ and <tt>t.dungeon</tt> in the above example refer to
# the same in-memory instance since the association matches the name of the class.
Expand Down
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/lib/active_record/core.rb
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ def init_with_attributes(attributes, new_record = false) # :nodoc:
# new_user.name = "Joe"
# user.name # => "Joe"
#
# user.object_id == new_user.object_id # => false
# user.name.object_id == new_user.name.object_id # => true
# user.__id__ == new_user.__id__ # => false
# user.name.__id__ == new_user.name.__id__ # => true
#
# user.name.object_id == user.dup.name.object_id # => false
# user.name.__id__ == user.dup.name.__id__ # => false

##
# :method: dup
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 1fca131

Please sign in to comment.