百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
R

ruby-destroyed_at

> 编程语言
开源

活动记录 Mixin 安全毁灭

345 stars0 点赞1 次浏览
访问官网GitHub

工具介绍

活动记录 Mixin 安全毁灭

DestroyedAt

Looking for help?

If it is a bug please open an issue on GitHub.

Installation

Add the destroyed_at gem to your Gemfile

ruby
gem 'destroyed_at'

You can either mixin the modules on a case-by-case basis:

ruby
class User  true
user.destroyed_at
# => 

Warning: Dependent relations with destroy

Be careful when destroying models that have dependent: :destroy. This will delete, not destroy, the associated model if said model does not include DestroyedAt.

Restoring

When you'd like to "restore" a record, call the #restore method on the instance. This will set its #destroyed_at value to nil, thereby including it in the default scope of the class again.

To include this functionality on has_many through relationships, be sure to include DestroyedAt on the through model, as well as the parent model.

ruby
class User  true
user.destroyed_at
# => nil

Destroyed Scope

When you include the DestroyedAt module, it sets a default scope of where(destroyed_at: nil). It also defines a scope called .destroyed which removes the default scope and finds the records of a relation where.not(destroyed_at: nil). This is different than calling unscoped on the class/relation. Where unscoped will remove all scoping from a relation, .destroyed only removes the default scope generated by the DestroyedAt module, making it safe to call on relations.

Callbacks

before_restore, after_restore and around_restore callbacks are added to your model. They work similarly to the before_destroy, after_destroy and around_restore callbacks.

ruby
class User  { where(destroyed_at: nil) } }

Rails will by default not include default_scopes when querying for uniqueness. Rather than monkey patching the validator we believe this is the best solution.

Authors

  • Michael Dupuis
  • Brian Cardarella

We are very thankful for the many contributors

Versioning

This gem follows Semantic Versioning

Want to help?

Please do! We are always looking to improve this gem. Please see our Contribution Guidelines on how to properly submit issues and pull requests.

Legal

DockYard Inc. © 2014

@dockyard

Licensed under the MIT license

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言