Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

ruby-destroyed_at

> 编程语言
Open source

ActiveRecord Mixin for Safe Destroys

345 stars0 likes1 views
WebsiteGitHub

About

ActiveRecord Mixin for Safe Destroys

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 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Ruby

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

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