Rails 3 - Removing a User but retaining content?
I've seemed to have gotten myself into a bit of a pickle. I'm trying to write a way to destroy users, but still retain their content.
Currently in my app, a company has many users, and users belongs to one company. A company can make a user inactive if they need to, but I'd also like for the company to be able to remove users from their account in cases of termination, etc.
I was going to simply, on destroy, set the foreign company_id key to nil in the user's table. However, by doing this, the company would lose this old user's work, something that can't hap开发者_如何学JAVApen, as information is populated from doing queries on the company_id.
Has anyone thought of a nice little way of getting around this with a similar DB design? Or will I have to go back to square one to figure out a way to do this more effectively.
Thanks
This is really more of an interface issue than a data model issue. To be sure, you shouldn't actually delete/destroy the users if you need to retain their information. But if you need a way of removing users apart from just making them inactive, you could add another option for a 'terminated' status. Then it just depends on how and where you display these terminated users in the front end (or maybe you just never display them).
The "soft delete" pattern is probably the simplest approach. In this scenario, you add a field with boolean semantics that means, essentially "active" or "deleted".
In order to minimize the impact to existing code, you could copy the existing data to a new table with the updated schema, and create a view with the same name as the old table, including only those records not marked as deleted; clients that didn't need to know about the "deleted" users would remain blissfully unaware of those records.
You might want to checkout acts as paranoid which does a soft delete of users by just setting a deleted flag. This way the row stays in the DB, but does not show up in any generic query results.
yes can checkout
rails3_acts_as_paranoid - which allows you to hide and restore records without actually deleting them.
This is best technique for your scenario.
精彩评论