Rails 4+ Best Practices: Remove Parents while Keeping Children

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

  • Save the existing parent and use the "deleted_at" field to indicate the parent itself is inactive, but the relationship still exists. This will result in the number of dead parent records being kept forever. Fur.
  • Assign all abandoned child records to the zombie collector's shared parent record. I prefer this, but then you lose the history in the original source of the child record.

I have no Rails experience to see ahead which of these 2 would be the most appropriate way, or maybe there would be a completely different solution.

SO tells me that this seems to be a subjective question, and they can close it. I hope not, because I'm sure this is a question that others have.

+4
source share
1 answer

It seems to me that you are mostly asking about the soft deletion functionality. When I want this behavior, I usually add an attribute activethat is equal by default true. I also add an area activeto the model, so I can do something like Salon.activeto conveniently get everything that is active.

So, I think my answer is that I would do something like # 1, which I would call soft deletion. Idea # 2 looks pretty crazy to me.

0

All Articles