Reputation: 15491
Having a social media type application in Rails 4 using Devise. Im looking for a good way to delete users from the system. Since Facebook allows users to reactivate there accounts I have now implemented this feature with a
user.deactivated
boolean, thus not actually deleting records from the database.
Using a rake task I now be able to filter all users that have not confirmed there account for 7 days, These users I would like to really remove from the database. Since users cannot do anything on my application without confirming, I consider them "lost" registrations.
I wonder what would be the best strategy to delete these users in a real removal from the database?
Since each user has its own profile/account what would be the best way to delete everything from the system? Im using:
belongs_to :user, :dependent => :destroy
In profile/account models so the deleting the user from database should delete all there other relations. Is this best way to go? Any things to take into consideration that would break the system?
Upvotes: 1
Views: 444
Reputation: 2853
As You already started going this putting dependencies between models is a good approach, this will remove everything linked on destroy
(don't mix up with delete
it executes SQL query without Rails callbacks). Though I started to avoid this technique in latest projects.
I am kind of old school about DB cleanliness, so we usually also use gems to enforce FK integrity. It adds another layer of maintenance when deleting objects (as it will throw SQL error when triggered), but I find it more reliable, then a single line in a model. Also we usually overcome this added complexity by providing models with custom_destroy
methods that executes delete
on all dependent objects.
def custom_destroy
self.class.transaction do
child_objects.delete_all
some_other_child_objects.delete_all
delete
end
end
This provides an advantage of speed, because using destroy_all on 10 child objects will execute 10 SQL queries (+ all the callbacks), this method on the other hand will do only one query.
We seldomly have a need to clean up old unneeded data (expired items), we use a rake tasks for that. If You are on Heroku simply use Heroku scheduler, if You are on a VPS/dedicated machine I advice on using whenever gem, it should have a good integration with Capistrano as well.
Upvotes: 1