Rating: depend =>: destroy

In Rails 2.2.2 (ruby 1.8.7-p72), I would like to evaluate the impact of destroying an object before it actually executes. That is, I would like to be able to generate a list of all the objects that will be affected by: depend =>: destroy (through object associations). The real problem I'm trying to solve is to provide the user with a list of everything that will be deleted and confirm their action.

Can someone recommend a good way to do this? I just started to learn ActiveRecord :: Associations, but I have not achieved much success.

Update. In my particular case, I have different levels of objects (A β†’ B β†’ C).

+4
source share
4 answers

This should help you get started ... Obviously, you will have to configure it, but it lists all the association names that depend on the destruction in the BlogEntry class:

BlogEntry.reflect_on_all_associations.map do |association| if association.options[:dependent] == :destroy # do something here... association.name end end.compact => [:taggings, :comments] 
+6
source

Just manually maintain a list of related objects with dependent destruction (maybe it doesn't matter what you need to do anyway), and then have named_scopes for each to pull the included objects for display.

+1
source

I would say that, as mentioned, there is a way to display the affected entries to the user, then there are two buttons / links, one of which is deleted, possibly with confirmation for the user who asks if they checked the other link, which is a list of all records that they will influence.

Then, if you want to be sure that you can also do a soft deletion by marking them as deleted in the database, rather than deleting them, which may come in handy, I don’t know how you can handle this automatic dependent deletion, possibly with act_as_paranoid or some kind of self-catalyzed version with a parent model callback.

0
source

I recently wrote a simple Rails plugin that solves this problem.
Check it out on github: http://github.com/murbanski/affected_on_destroy/tree

0
source

All Articles