我关注了这些 instructions在登录时检查用户是否已被软删除。在下面的示例中,我可以检查 bool 值:
Class User < ActiveRecord::Base
def self.find_for_authentication(conditions)
super(conditions.merge(:deleted_flag => false))
end
我更喜欢时间戳 (created_at) 字段。我如何检查这样的字段是否为空?数据库为以下检查抛出错误:
super(conditions.merge(:deleted_at => false)) # also tried nil, "NULL" and "IS NULL"
最佳答案
当然,您不能在不修改设计的情况下使用此解决方案。 Devise 会将您的条件直接发送到数据库,因此无法调用方法或使用像 squeel 这样的库(这将允许类似 where{created_at == nil}
的东西。
您可以使用 How to "soft delete" user with Devise 中提供的解决方案,但错误消息将是:“您必须在继续之前确认您的帐户。”
将此添加到您的资源模型:
def inactive_message
!!deleted_at ? :deleted : super
end
并向您的语言环境添加一条消息:
en:
devise:
failure:
deleted: "Your account was deleted."
希望对您有所帮助!
关于ruby-on-rails - PostgreSQL 检查时间戳字段是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12936821/