What is the difference between dependent: :destroy and dependent: :delete_all?

Answer

Both options on has_many remove associated records when the parent is deleted, but differently. dependent: :destroy loads each associated record and calls destroy on it — this fires each record's callbacks (before_destroy, after_destroy, dependent associations cascade down). dependent: :delete_all issues a single DELETE FROM table WHERE parent_id = ? SQL statement — much faster but bypasses all callbacks and validations. Use :destroy when you need callbacks to fire (e.g., deleting files, cascading associations). Use :delete_all for simple cleanup of large datasets without side effects.