Don't validate foreign key in required belongs_to

Before this change, every time we saved a record, the association was
validated if we had `belongs_to :something, required: true`. After this
change [1], it's only validated if the `something_id` column is nil (or
`something_type` for polymorphic associations) or if the `something_id`
attribute has changed.

The main difference is that we no longer get validation errors if the
associated record has been deleted. Doesn't affect us much, so we're
going with the new default value.

[1] Pull request 46522 in https://github.com/rails/rails
This commit is contained in:
Javi Martín
2024-04-15 15:21:56 +02:00
parent 364fa2603c
commit 529b4d2a98

View File

@@ -165,7 +165,7 @@ Rails.application.config.active_record.raise_on_assign_to_attr_readonly = true
# The previous behavior was to validate the presence of the parent record, which performed an extra query # The previous behavior was to validate the presence of the parent record, which performed an extra query
# to get the parent every time the child record was updated, even when parent has not changed. # to get the parent every time the child record was updated, even when parent has not changed.
#++ #++
# Rails.application.config.active_record.belongs_to_required_validates_foreign_key = false Rails.application.config.active_record.belongs_to_required_validates_foreign_key = false
### ###
# Enable precompilation of `config.filter_parameters`. Precompilation can # Enable precompilation of `config.filter_parameters`. Precompilation can