This is probably a complicated one, but I'll give it a try. We have a Rails app - peer-to-peer marketplace with lots of registered users in beta testing. Users have many items for sale and swap. User will get offers from other users that want to buy or swap their items for user's items. Those items might also be wished by other users. Items have many likes and comments from other users and there are also notifications implemented for each action. In short there's lots of one-to-many, many-to-many and polymorphic relations in Users and Items table.
Some sneak peak into model complexity:
class Item < ActiveRecord::Base
belongs_to :user
has_many :wishes, as: :wishable
has_many :listing_likes, foreign_key: 'listing_id', class_name: 'ListingLike'
The question is, what's the best way to approach user account deletion and keeping Data Integrity in this case. Hard deleting and dependent: destroy
is not an option, because it will break the user experience to other registered users.
The only thing that comes to mind is to add deleted
boolean field to users table and to create a check function if the user was deleted, including this function basically everywhere in the app just to make sure that this user wasn't soft deleted and to display a greyed out user name that is not clickable anymore. That approach will probably take too long to implement and will be pretty hard to maintain.
Any advice for better approach will be appreciated. Is there a common approach, best practice or just an advice for how to treat it the less painful way?
Thanks in advance
I think your initial thought of implementing some form of "soft delete" is the way to go. When you build a complex network-like data structure you can't (and often don't want) to remove a single piece.
You might be surprised at how easy this is to implement yourself, but you don't need to implement it yourself: check out Paranoia, which implements a fairly robust approach to soft deletion.