model(1).rb
class model(1) < ApplicationRecord
has_many :model(Many), dependent: :delete_all
end
-Delete model1 = Linked data will also be deleted at the same time ・ When the parent is deleted, the children are automatically deleted. -Example): When the column information of the parent question is deleted, the column information of the answer associated with it is also deleted.
model(1).rb
class model(1) < ApplicationRecord
has_many :model(Many), dependent: :destroy
end
-The same process as delete_all
is performed, but the objects are deleted one by one.
-Performance is lower than delete_all
・ If there are two or more layers of association, you may not be able to do it without using this.
delete_all
when one level is used)-The linking automatic deletion function is valid only during the destroy action. -It is necessary to change the delete method of the controller to the destroy method.
Recommended Posts