40

I get the following error in Rails 4:

dependent option must be one of destroy delete

Per https://github.com/rails/rails/issues/3458, other options were supported in older versions. But what is possible nowadays? I could not find any other documentation.

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
Georg Heiler
  • 16,916
  • 36
  • 162
  • 292
  • Can you show us the line of code causing this error? which options are available depend on what kind of association you are defining... Note: edit your question, and add the code there, do't just reply int he comments... (code formatting in comments is awful, and the relevant code really should be part of your original question) – Taryn East Sep 22 '14 at 06:48

2 Answers2

86

Docs are available here

Looks like the following options are supported:

  • nil - do nothing (default).

  • :destroy - causes all the associated objects to also be destroyed.

  • :delete_all - causes all the associated objects to be deleted directly from the database (so callbacks will not be executed).

  • :nullify - causes the foreign keys to be set to NULL. Callbacks are not executed.

  • :restrict_with_exception - causes an exception to be raised if there are any associated records.

  • :restrict_with_error - causes an error to be added to the owner if there are any associated objects.

notapatch
  • 6,569
  • 6
  • 41
  • 45
Ben
  • 2,096
  • 15
  • 9
  • 4
    Worth noting perhaps that the default behaviour (when there's a database-level unique constraint, and no `dependent` configured) is to catch the database-level failure and raise `ActiveRecord::InvalidForeignKey`. The `restrict_with_exception` option is closest in behaviour to this, but it raises a (more descriptive) `ActiveRecord::DeleteRestrictionError` before the database transaction is even attempted. – Ollie Bennett Jun 28 '18 at 12:20
2

Adding to Ben's Answer, if it is required to do nothing on deletion, nil (which is default behaviour) can also be used