views:

190

answers:

2

Or, put differently, is there any reason not to use it on all of my models?

Some background: is_paranoid is a gem that makes calls to destroy set a deleted_at timestamp rather than deleting the row (and calls to find exclude rows with non-null deleted_ats).

I've found this feature so useful that I'm starting to include it in every model -- hard deleting records is just too scary. Is there any reason this is a bad thing? Should this feature be turned on by default in Rails?

+3  A: 

Ruby is not for cowards who are scared of their own code!

In most cases you really want to delete the record completely. Consider a table that contains relationships between two other models. This is an obvious case when you would not like to use deleted_at.

Another thing is that your approach to database design is kinda rubyish. You will suffer of necessity to handle all this deleted_At stuff, when you have to write more complex queries to your tables than mere finds. And you surely will, when your application's DB takes lots of space so you'll have to replace nice and shiny ruby code with hacky SQL queries. You may want then to discard this column, but--oops--you have already utilized deleted_at logic somewhere and you'll have to rewrite larger pieces of your app. Gotcha.

And at the last place, actually it seems natural when things disappear upon deletion. And the whole point of the modelling is that the models try to express in machine-readable terms what's going on there. By default you delete record and it passes forever. And only reason deleted_at may be natural is when a record is to be later restored or should prevent similar record to be confused with the original one (table for Users is most likely the place you want to use it). But in most models it's just paranoia.

What I'm trying to say is that the plausibility to restore deleted records should be an explicitly expressed intent, because it's not what people normally expect and because there are cases where implicit use of it is error prone and not just adds a small overhead (unlike maintaining a created_at column).

Of course, there is a number of cases where you would like to revert deletion of records (especially when accidental deletion of valuable data leads to an unnecessary expenditure). But to utilize it you'll have to modify your application, add forms an so on, so it won't be a problem to add just another line to your model class. And there certainly are other ways you may implement storing deleted data.

So IMHO that's an unnecessary feature for every model and should be only turned on when needed and when this way to add safety to models is applicable to a particular model. And that means not by default.

(This past was influenced by railsninja's valuable remarks).

Pavel Shved
This is nonsense. Addressed in my answer.
railsninja
A: 

@Pavel Shved

I'm sorry but what? Ruby is not for cowards scared of code? This could be one of the most ridiculous things I have ever heard. Sure in a join table you want to delete records, but what about the join model of a has many through, maybe not.

In Business applications it often makes good sense to not hard delete things, Users make mistakes, A LOT.

A lot of your response, Pavel, is kind of dribble. There is no shame in using SQL where you need to, and how does using deleted_at cause this massive refactor, I'm at a loss about that.

@Horace I don't think is_paranoid should be in core, not everyone needs it. It's a fantastic gem though, I use it in my work apps and it works great. I am also fairly certain it hasn't forced me to resort to sql when I wouldn't need to, and I can't see a big refactor in my future due to it's presence. Use it when you need it, but it should be a gem.

railsninja
Ah, "Business". I should feel like a child after hearing that and immediately delete my answer, right? :-) My points were: (1) "I'm scared" is not the reason for making decision; (2) there are certain cases where you absolutely don't want is_paranoid; (3) Horace *probably* is not aware that writing SQL is not a shame and *probably* thinks he won't have to do this ever. Whereas using is_paranoid everywhere would make writing SQL more difficult even if it's not utilized.
Pavel Shved
I agree that is_paranoid shouldn't be in core. Your answer is not good, and again, who are you to say the records should definately be deleted most of time, it's totally situational. I was not trying to make you feel like a child, although you do act like one. My point was in a "business" the code and data are not yours, sometimes better safe than sorry. I hope you don't misguide too many people.
railsninja
Ok, you make a somewhat right point, I'll add clarifications to my post.
Pavel Shved