使用 act_as_paranoid 插件进行软删除 - 验证怎么样? [英] Using acts_as_paranoid plugin for soft delete - what about validations?

查看:36
本文介绍了使用 act_as_paranoid 插件进行软删除 - 验证怎么样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 act_as_paranoid 插件来软删除记录.我早些时候使用数据库中的标志来管理它.我知道这个插件会从搜索中省略记录并查找记录是否被软删除.我想知道的是,我是否在模型中进行了验证,例如 validates_uniqueness_of :email 并且我删除(软删除)了电子邮件为prince@gmail.com"的记录.现在,当我尝试创建具有相同电子邮件的新用户时,验证是否有效并阻止创建新记录.还是会像查找时一样省略软删除的记录?(当然,我希望这种情况发生.)

I am looking on trying to use acts_as_paranoid plugin for soft delete of records. I was earlier managing it using a flag in the db. I know that this plugin will omit a record from searches and finds if the record is soft deleted. What I want to know is if I have a validation in the model like validates_uniqueness_of :email and I deleted(soft deleted) the record having email 'prince@gmail.com'. Now when I try to create a new user having same email, will the validation work and prevents the creation of the new record. Or will it omit the soft deleted record as it does for finds? (I would like this to happen, of course.)

推荐答案

acts_as_paranoid 不会重新实现 validates_uniqueness_of,因此如果您(软)删除了带有电子邮件的记录 'Prince@gmail.com' 您不能使用相同的电子邮件创建新记录.

acts_as_paranoid does not reimplement validates_uniqueness_of, so if you have (soft) deleted a record with email 'prince@gmail.com' you cannot create a new record with the same email.

解决这个问题的简单方法是为 validates_uniqueness_of 添加一个范围:

The easy fix for this is to add a scope to validates_uniqueness_of:

validates_uniqueness_of :email, :scope => :deleted_at

通过这种方式,您可以使用电子邮件prince@gmail.com"删除任意数量的(软)记录,并且仍然使用同一电子邮件创建新记录.

This way you can have any number of (soft) deleted records with email 'prince@gmail.com' and still create a new record with the same email.

这篇关于使用 act_as_paranoid 插件进行软删除 - 验证怎么样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆