Open kirykr opened 4 years ago
@kirykr just to double check, you would want:
Correct?
@mvz Yes , you're correct.
I have a similar issue...
class Parent < ActiveRecord::Base
has_many :children, dependent: :destroy
end
class Child < ActiveRecord::Base
acts_as_paranoid
belongs_to :parent
end
Parent.first.destroy
ActiveRecord::InvalidForeignKey: PG::ForeignKeyViolation: ERROR: update or delete on table "parents" violates foreign key constraint "fk_rails_b38590a748" on table "children"
DETAIL: Key (id)=(aa67c98c-d81f-5a9c-b0bc-26caa0051aea) is still referenced from table "children".
Since Parent
is not paranoid I would expect calling #destroy
to work and fully destroy its children.
Hi @szechyjs your case is different enough that it should be in a separate issue. Can you open one please?
Just checking in if there will be any motion on this or if it's abandoned?
I'm thinking of doing something like this in the mean time... thoughts?
class ApplicationRecord < ActiveRecord::Base
# override fully_destroy! to destroy all associated objects with dependent: :destroy
# @returns [Boolean] true if destroyed
def destroy_fully_with_dependencies!
dependent_associations = self.class.reflect_on_all_associations(:has_many).select { |a| a.options[:dependent] == :destroy }
dependent_associations.each do |association|
self.send(association.name).each(&:destroy_fully_with_dependencies!)
end
self.destroy_fully!
end
@Genkilabs that should work. Be sure to create some tests for it!
Is there anythinng wrong here? Thanks