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.
The dependent: option which is built into Rails allows us to specify what happens to the associated records when their owner is destroyed. Until now, the dependent: option accepted :destroy , :delete_all amongst other values.
There is no difference between the two; :dependent => :destroy and :dependent => :delete_all are semantically equivalent. In :destroy, associated objects are destroyed alongside the object by calling their :destroy method, while in :delete_all, they are destroyed immediately, without calling their :destroy method.
Dependent is an option of Rails collection association declaration to cascade the delete action. The :destroy is to cause the associated object to also be destroyed when its owner is destroyed.
They essentially do the same thing, the only difference is what side of the relationship you are on. If a User has a Profile , then in the User class you'd have has_one :profile and in the Profile class you'd have belongs_to :user . To determine who "has" the other object, look at where the foreign key is.
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.
Adding to Ben's Answer, if it is required to do nothing on deletion, nil (which is default behaviour) can also be used
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With