Skip to content

Commit 4bf2c4c

Browse files
committed
Remove deprecated behavior to support referring to a singular association by its plural name
1 parent 9ef7938 commit 4bf2c4c

File tree

7 files changed

+26
-69
lines changed

7 files changed

+26
-69
lines changed

activerecord/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
* Remove deprecated behavior to support referring to a singular association by its plural name.
2+
3+
*Rafael Mendonça França*
4+
5+
* Deprecate `Rails.application.config.active_record.allow_deprecated_singular_associations_name`
6+
7+
*Rafael Mendonça França*
8+
19
* Remove deprecated support to passing `SchemaMigration` and `InternalMetadata` classes as arguments to
210
`ActiveRecord::MigrationContext`.
311

activerecord/lib/active_record.rb

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -422,12 +422,19 @@ def self.global_executor_concurrency # :nodoc:
422422
singleton_class.attr_accessor :verify_foreign_keys_for_fixtures
423423
self.verify_foreign_keys_for_fixtures = false
424424

425-
##
426-
# :singleton-method: allow_deprecated_singular_associations_name
427-
# If true, Rails will continue allowing plural association names in where clauses on singular associations
428-
# This behavior will be removed in Rails 7.2.
429-
singleton_class.attr_accessor :allow_deprecated_singular_associations_name
430-
self.allow_deprecated_singular_associations_name = true
425+
def self.allow_deprecated_singular_associations_name
426+
ActiveRecord.deprecator.warn <<-WARNING.squish
427+
`Rails.application.config.active_record.allow_deprecated_singular_associations_name`
428+
is deprecated and will be removed in Rails 7.3.
429+
WARNING
430+
end
431+
432+
def self.allow_deprecated_singular_associations_name=(value)
433+
ActiveRecord.deprecator.warn <<-WARNING.squish
434+
`Rails.application.config.active_record.allow_deprecated_singular_associations_name`
435+
is deprecated and will be removed in Rails 7.3.
436+
WARNING
437+
end
431438

432439
singleton_class.attr_accessor :query_transformers
433440
self.query_transformers = []

activerecord/lib/active_record/table_metadata.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,7 @@ def has_column?(column_name)
2323
end
2424

2525
def associated_with?(table_name)
26-
if reflection = klass&._reflect_on_association(table_name)
27-
reflection
28-
elsif ActiveRecord.allow_deprecated_singular_associations_name && reflection = klass&._reflect_on_association(table_name.singularize)
29-
ActiveRecord.deprecator.warn(<<~MSG)
30-
Referring to a singular association (e.g. `#{reflection.name}`) by its plural name (e.g. `#{reflection.plural_name}`) is deprecated.
31-
32-
To convert this deprecation warning to an error and enable more performant behavior, set config.active_record.allow_deprecated_singular_associations_name = false.
33-
MSG
34-
reflection
35-
end
26+
klass&._reflect_on_association(table_name)
3627
end
3728

3829
def associated_table(table_name)

guides/source/7_2_release_notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,12 @@ Please refer to the [Changelog][active-record] for detailed changes.
134134
* Remove deprecated support to passing `SchemaMigration` and `InternalMetadata` classes as arguments to
135135
`ActiveRecord::MigrationContext`.
136136

137+
* Remove deprecated behavior to support referring to a singular association by its plural name.
138+
137139
### Deprecations
138140

141+
* Deprecate `Rails.application.config.active_record.allow_deprecated_singular_associations_name`
142+
139143
### Notable changes
140144

141145
Active Storage

guides/source/configuring.md

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ Below are the default values associated with each target version. In cases of co
6969
- [`config.action_dispatch.default_headers`](#config-action-dispatch-default-headers): `{ "X-Frame-Options" => "SAMEORIGIN", "X-XSS-Protection" => "0", "X-Content-Type-Options" => "nosniff", "X-Permitted-Cross-Domain-Policies" => "none", "Referrer-Policy" => "strict-origin-when-cross-origin" }`
7070
- [`config.action_text.sanitizer_vendor`](#config-action-text-sanitizer-vendor): `Rails::HTML::Sanitizer.best_supported_vendor`
7171
- [`config.action_view.sanitizer_vendor`](#config-action-view-sanitizer-vendor): `Rails::HTML::Sanitizer.best_supported_vendor`
72-
- [`config.active_record.allow_deprecated_singular_associations_name`](#config-active-record-allow-deprecated-singular-associations-name): `false`
7372
- [`config.active_record.before_committed_on_all_records`](#config-active-record-before-committed-on-all-records): `true`
7473
- [`config.active_record.belongs_to_required_validates_foreign_key`](#config-active-record-belongs-to-required-validates-foreign-key): `false`
7574
- [`config.active_record.commit_transaction_on_non_local_return`](#config-active-record-commit-transaction-on-non-local-return): `true`
@@ -1509,31 +1508,6 @@ For each process, Rails will create one global query executor that uses this man
15091508
should be at least `thread_count + global_executor_concurrency + 1`. For example, if your web server has a maximum of 3 threads,
15101509
and `global_executor_concurrency` is set to 4, then your pool size should be at least 8.
15111510

1512-
#### `config.active_record.allow_deprecated_singular_associations_name`
1513-
1514-
This enables deprecated behavior wherein singular associations can be referred to by their plural name in `where` clauses. Setting this to `false` is more performant.
1515-
1516-
```ruby
1517-
class Comment < ActiveRecord::Base
1518-
belongs_to :post
1519-
end
1520-
1521-
Comment.where(post: post_id).count # => 5
1522-
1523-
# When `allow_deprecated_singular_associations_name` is true:
1524-
Comment.where(posts: post_id).count # => 5 (deprecation warning)
1525-
1526-
# When `allow_deprecated_singular_associations_name` is false:
1527-
Comment.where(posts: post_id).count # => error
1528-
```
1529-
1530-
The default value depends on the `config.load_defaults` target version:
1531-
1532-
| Starting with version | The default value is |
1533-
| --------------------- | -------------------- |
1534-
| (original) | `true` |
1535-
| 7.1 | `false` |
1536-
15371511
#### `config.active_record.yaml_column_permitted_classes`
15381512

15391513
Defaults to `[Symbol]`. Allows applications to include additional permitted classes to `safe_load()` on the `ActiveRecord::Coders::YAMLColumn`.

railties/lib/rails/application/configuration.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,6 @@ def load_defaults(target_version)
280280
if respond_to?(:active_record)
281281
active_record.run_commit_callbacks_on_first_saved_instances_in_transaction = false
282282
active_record.commit_transaction_on_non_local_return = true
283-
active_record.allow_deprecated_singular_associations_name = false
284283
active_record.sqlite3_adapter_strict_strings_by_default = true
285284
active_record.query_log_tags_format = :sqlcommenter
286285
active_record.raise_on_assign_to_attr_readonly = true

railties/test/application/configuration_test.rb

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2875,32 +2875,6 @@ def index
28752875
assert_equal true, ActiveRecord.verify_foreign_keys_for_fixtures
28762876
end
28772877

2878-
test "ActiveRecord.allow_deprecated_singular_associations_name is false by default for new apps" do
2879-
app "development"
2880-
2881-
assert_equal false, ActiveRecord.allow_deprecated_singular_associations_name
2882-
end
2883-
2884-
test "ActiveRecord.allow_deprecated_singular_associations_name is true by default for upgraded apps" do
2885-
remove_from_config '.*config\.load_defaults.*\n'
2886-
2887-
app "development"
2888-
2889-
assert_equal true, ActiveRecord.allow_deprecated_singular_associations_name
2890-
end
2891-
2892-
test "ActiveRecord.allow_deprecated_singular_associations_name can be configured via config.active_record.allow_deprecated_singular_associations_name" do
2893-
remove_from_config '.*config\.load_defaults.*\n'
2894-
2895-
app_file "config/initializers/new_framework_defaults_7_1.rb", <<-RUBY
2896-
Rails.application.config.active_record.allow_deprecated_singular_associations_name = false
2897-
RUBY
2898-
2899-
app "development"
2900-
2901-
assert_equal false, ActiveRecord.allow_deprecated_singular_associations_name
2902-
end
2903-
29042878
test "ActiveRecord::Base.run_commit_callbacks_on_first_saved_instances_in_transaction is false by default for new apps" do
29052879
app "development"
29062880

0 commit comments

Comments
 (0)