Georg Heiler
Georg Heiler

Reputation: 17694

Rails has_one / has_many, dependent option: What values are available?

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.

Upvotes: 43

Views: 23040

Answers (2)

Ben
Ben

Reputation: 2156

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.

Upvotes: 91

Suleman Uzair
Suleman Uzair

Reputation: 69

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

Upvotes: 3

Related Questions