Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails dependent which options are possible?

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.

like image 323
Georg Heiler Avatar asked Sep 21 '14 18:09

Georg Heiler


People also ask

What is dependent in Rails?

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.

What is the difference between dependent => destroy and dependent => Delete_all in Rails?

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.

What is dependent destroy Rails?

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.

What is the difference between Has_one and Belongs_to?

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.


2 Answers

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.

like image 116
Ben Avatar answered Oct 17 '22 07:10

Ben


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

like image 29
Suleman Uzair Avatar answered Oct 17 '22 09:10

Suleman Uzair