ruby on rails - Soft deleting huge user accounts with a background task -


i'm using rails , devise app stores lot of data on users. want make possible user deactivate account similar way facebook if log in, account gets reactivated. far i've tackled using soft delete. problem is, when people delete accounts, there's data needs soft deleted takes while run. naturally, instinct use delayed_job this. problem is, works on account deletion, not on reactivation. don't want users have sit around 10 second while data restored, can't in background, because they'll logged in before of data has been restored.

any ideas how go solving problem?

thanks in advance!

you'll want set soft_delete tracked boolean flag on relevant records. set default scopes return records don't have flag set. when comes time activate or deactivate, gather relevant records , hit them update_all. here's example run against 13,000 user records give sense of time & performance:

1.9.2p320 :001 > user.update_all(soft_deleted: false)   sql (1016.3ms)  update "users" set "soft_deleted" = 'f'  => 13350  

as see, hit 13,000 records flag toggle in 1 second. so, if wanted hit user, of user's posts, , of users privatemessages,

user.update_attributes(soft_deleted: true) user.posts.update_all(soft_deleted: true) user.private_messages.update_all(soft_deleted: true) 

and should go. if you're dealing many records technique doesn't perform well, don't think you're going have choice except tell user may few moments before data available on reactivation , throw whole process background job, planned.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -