Rails: delete cascade vs dependent destroy

has_many :orders, dependent: :destroy

  • Safest option for automatically maintaining data integrity.
  • You have polymorphic associations, and do not want to use triggers.


add_foreign_key :orders, :users, on_delete: :cascade (in database migration)

  • You are not using any polymorphic associations or you want to use triggers for each polymorphic association.


has_many :orders, dependent: :delete_all

  • Only use when the has_many is a leaf node on your association tree (i.e. the child does not have another has_many association with foreign key references)

I would use option 1. While it may work, I can see a number of issues with option 2:

  1. ActiveRecord will be unaware that these records were deleted, which could lead to unstable behavior
  2. it would be unclear to anyone reading the code that deleting a user means that all their orders will also be deleted
  3. any destroy handlers on Order would not fire

Certainly I would expect option 2 to be faster, but it's up to you if the trade-offs are worth it. Is deletion of a user a common operation in your application?

Another option would be to use :dependent => :delete_all. This would be faster than :dependent => :destroy and avoid drawbacks 1 and 2 above. See here for more details.


It really depends on the behavior you want. In case 1, destroy will be called on each associated order, and therefor so will the ActiveRecord callbacks. In case 2, these callbacks are not triggered, but it will be way faster and guarantees referential integrity.

In an application's infancy, I'd recommend going with :dependent => :destroy because it lets you develop in a way that is independent of the database. Once you start to scale, you should start doing it in the database for performance/integrity reasons.