[英]In Rails 4, how would you log and undo all database changes specific to one user?
I'm trying to set a specific user account as being "sandboxed", meaning I want to roll back any database changes that user makes when their session is destroyed. 我试图将特定的用户帐户设置为“沙盒”,这意味着我想回滚用户在会话被销毁时所做的所有数据库更改。
I looked at using the Paper Trail gem, but I'm not sure it can be used to roll back changes specific to one user. 我看过使用Paper Trail宝石,但是我不确定它是否可以用于回滚特定于一个用户的更改。
Is this possible? 这可能吗?
One way to do it is to have an activities table where you log all activities by user. 一种方法是拥有一个活动表,您可以在其中按用户记录所有活动。 Then, when a sandboxed user is deleted, you can go through the activities of that user, delete the corresponding entities.
然后,当删除沙盒用户时,您可以浏览该用户的活动,删除相应的实体。
bundle g model Activity user:references thing:references{polymorphic}:index
And when session is destroyed: 当会话被破坏时:
current_user_activities = Activity.where(user_id: current_user.id)
current_user_activities.each do |activity|
activity.thing.destroy!
end
current_user_activities.destroy_all
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.