What records are affected by ActiveRecord depend: destroy?

I have several ActiveRecord objects with associations marked as dependent: :destroy . When a user deletes one of these objects, I want to present him with a list of all objects that will be (recursively) deleted. How to get this list?

I found the affected_on_destroy gem here , but it is not quite finished and it seems rails 2.2. Is there any other new stone or some feature in the rails?

+4
source share
2 answers

I wrote gem affected_by_destroy , the source can be found here .

+1
source
 Class.reflect_on_all_associations. keep_if{ |assoc| assoc.options[:dependent] == :destroy }.map(&:name) 

It may not be exactly what you are looking for, but I think that reflect_on_all_associations should provide you with what you want.

+1
source

Source: https://habr.com/ru/post/1433323/


All Articles