4

I want to keep child records and the hierarchy, even when the parent is deleted. I see two options:

  1. Keep the existing parent and utilize a "deleted_at" field to indicate the parent itself is inactive, but the relationship still exists. This will lead to a number of effectively dead parent records being stored forever. Meh.
  2. Assign all abandoned child records to a generic "collector" zombie parent record. I prefer this, but then you lose the history to the original source of the child record.

I don't have the Rails experience to see ahead as to which of these 2 would be the most advisable path to take, or maybe there's an altogether different solution.

SO is telling me this appears to be a subjective question and they may close it. I hope not, because I'm sure this is a question that others have as well.

Elvn
  • 2,781
  • 1
  • 12
  • 27
  • 1
    You can take a look at this answer: http://stackoverflow.com/questions/23017070/how-to-hide-records-rather-than-delete-them-soft-delete-from-scratch/23017174#23017174 – MrYoshiji Feb 17 '15 at 18:13
  • This also may be interesting: http://stackoverflow.com/questions/378331/physical-vs-logical-soft-delete-of-database-record – Jason Swett Feb 17 '15 at 18:18
  • Thanks guys. Those are both relevant references, and helpful. It does seem like best practice is to go with the soft delete over the hard delete. – Elvn Feb 17 '15 at 18:28

1 Answers1

0

It seems to me like you're basically asking about "soft delete" functionality. When I want this kind of behavior, I usually add an active attribute that defaults to true. I also add an active scope to the model so I can do something like Salon.active to conveniently get everything that's active.

So I guess my answer is that I'd do something like #1, which I would call a soft delete. Idea #2 seems pretty crazy to me.

Jason Swett
  • 38,405
  • 60
  • 193
  • 322