简体   繁体   English

在Rails 4中,您将如何记录和撤消特定于一个用户的所有数据库更改?

[英]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.

相关问题 如何撤消删除Rails中的脚手架? - how do you undo delete scaffold in rails? Rails关联–您将如何应用此用户关联 - Rails associations – How would you apply this user association 您将如何在Rails中做到这一点? - how would you do this in Rails? 在数据库级别进行Rails撤消 - Rails undo at database level 在用Rails编程的类似Facebook的社交网络中,您将用户“喜欢”页面存储在数据库的哪个位置? - In a Facebook-like social network programmed in Rails, where in the database would you store that a user “likes” a Page? 如何使用Rails 4轮询数据库中的一个值,如果值更改,请使用AJAX刷新 - How to poll one value in a database with Rails 4, if value changes, refresh with AJAX 用户在 Rails 中注销后如何使所有会话无效? - How to invalidate all sessions after user log out in Rails? 如何使用ActionCable将广播流传输到Rails 5中的特定用户? - How do you stream a broadcast to a specific user in Rails 5 with ActionCable? Rails:分析页面上的用户更改以插入数据库 - Rails: Analyze User changes on a page to insert to Database Rails gem:保留所有版本的数据库对象并保存与用户链接的更改的最佳方法是什么? - Rails gem: What is the best way to keep all versions of database objects and to save changes linked to a user?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM