This checker identifies cases where a belongs_to association is backed by a foreign key without an on_delete option. While the foreign key enforces data integrity by preventing orphaned records, attempting to delete the parent object will raise an error due to the database constraint.
To avoid such errors and handle deletions gracefully, you should either:
- Add a
dependent::destroy,:delete,:delete_all, or:nullifyoption to the correspondinghas_oneorhas_manyassociation. - Set the foreign key's
on_deleteoption tocascadeornullify.
Use case example:
class User
has_many :posts
end
class Post
belongs_to :user
end
# schema.rb
add_foreign_key "posts", "users"