Rails 3 是否有更好的版本,例如acts_as_paranoid?
有谁知道一个宝石,它既可以从数据库中软删除记录(只是将它们标记为已删除,而不是实际删除),并且......
还有什么时候您任何类型的查找都会自动忽略那些被删除的记录而不使用任何特殊的用户定义范围。例如,这一切都发生在宝石中。
干杯!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
DHH 热衷于用“关注点”来做这件事,这些“关注点”基本上只是模块混合,我喜欢这种极简主义的方法。
这是 DHH 的版本 - https://gist.github.com/1014971
这是我的错误修复版本 - https://gist.github.com/4032984
DHH is a fan of doing this with 'concerns' which are basically just module mixins and I like that minimalist approach.
Here's DHH's version - https://gist.github.com/1014971
Here's my bugfixed version - https://gist.github.com/4032984
您可能需要查看 paper_trail
https://github.com/airblade/paper_trail
You might want to checkout paper_trail
https://github.com/airblade/paper_trail
偏执
Radar 的“偏执狂”宝石正是您所寻找的。
它本质上是 acts_as_paranoid 的重写(并使用许多相同的约定),但更小、更快并且兼容Rails 3 和 4。
它可以完成您的两个请求:
当删除时自动设置
deleted_at
时间戳在类包含acts_as_paranoid
的对象上调用destroy
方法。自动对任何查询设置条件以确保
deleted_at IS NULL
,除非您在查询链中包含with_deleted
。关于图书馆或雷达的好话真是说不完。
奖励:他的文档可能是“行业”中最好的。
Paranoia
The Paranoia gem from the overtly talented Radar is what you'd be looking for.
It's essentially a rewrite of acts_as_paranoid (and uses many of the same conventions) but is smaller, faster and compatible with Rails 3 and 4.
It accomplishes both of your requests:
Automatically sets a
deleted_at
timestamp when adestroy
method is called on an object whose class includesacts_as_paranoid
.Automatically sets a condition on any queries that ensure
deleted_at IS NULL
, unless you includewith_deleted
in your query chain.Couldn't say enough good things about the library or Radar.
Bonus: His documentation might be the best in the "business".
paper_trail 有很多功能并且经过了良好的测试(已经存在很长时间了)。
如果您只需要指定的功能,请查看:
https://github.com/grosser/soft_deletion
paper_trail has lots of features and is well tested (been around for a long time).
If you want just the specified functionality, check out:
https://github.com/grosser/soft_deletion
我使用丢弃宝石。这是软删除记录的一种非常简单的方法。
https://www.rubydoc.info/gems/discard/1.0.0
I use Discard gem. It's a quite simple way for soft deletion records.
https://www.rubydoc.info/gems/discard/1.0.0