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 c3d1e87
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 18 deletions.
25 changes: 15 additions & 10 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
* In cases where MySQL returns `warning_count` greater than zero, but returns no warnings when
* Allow using `object_id` as a database column name.
It was available before rails 7.1 and may be used as a part of polymorphic relationship to `object` where `object` can be any other database record.

*Mikhail Doronin*

* In cases where MySQL returns `warning_count` greater than zero, but returns no warnings when
the `SHOW WARNINGS` query is executed, `ActiveRecord.db_warnings_action` proc will still be
called with a generic warning message rather than silently ignoring the warning(s).

*Kevin McPhillips*

* `DatabaseConfigurations#configs_for` can accept a symbol in the `name` parameter.
* `DatabaseConfigurations#configs_for` can accept a symbol in the `name` parameter.

*Andrew Novoselac*

* Fix `where(field: values)` queries when `field` is a serialized attribute
* Fix `where(field: values)` queries when `field` is a serialized attribute
(for example, when `field` uses `ActiveRecord::Base.serialize` or is a JSON
column).

*João Alves*

* Make the output of `ActiveRecord::Core#inspect` configurable.
* Make the output of `ActiveRecord::Core#inspect` configurable.

By default, calling `inspect` on a record will yield a formatted string including just the `id`.

Expand Down Expand Up @@ -45,17 +50,17 @@

*Andrew Novoselac*

* Don't mark Float::INFINITY as changed when reassigning it
* Don't mark Float::INFINITY as changed when reassigning it

When saving a record with a float infinite value, it shouldn't mark as changed

*Maicol Bentancor*

* Support `RETURNING` clause for MariaDB
* Support `RETURNING` clause for MariaDB

*fatkodima*, *Nikolay Kondratyev*

* The SQLite3 adapter now implements the `supports_deferrable_constraints?` contract
* The SQLite3 adapter now implements the `supports_deferrable_constraints?` contract

Allows foreign keys to be deferred by adding the `:deferrable` key to the `foreign_key` options.

Expand All @@ -66,7 +71,7 @@

*Stephen Margheim*

* Add `set_constraints` helper for PostgreSQL
* Add `set_constraints` helper for PostgreSQL

```ruby
Post.create!(user_id: -1) # => ActiveRecord::InvalidForeignKey
Expand All @@ -82,11 +87,11 @@

*Cody Cutrer*

* Include `ActiveModel::API` in `ActiveRecord::Base`
* Include `ActiveModel::API` in `ActiveRecord::Base`

*Sean Doyle*

* Ensure `#signed_id` outputs `url_safe` strings.
* Ensure `#signed_id` outputs `url_safe` strings.

*Jason Meller*

Expand Down
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 c3d1e87

Please sign in to comment.