PostgreSQL 检查时间戳字段是否为空 [英] PostgreSQL check whether timestamp field is empty
问题描述
我遵循了这些说明 在登录时检查用户是否已被软删除.在下面的示例中,我可以检查布尔值:
I followed these instructions to check whether a user has been soft-deleted or not when logging in. In the example below I can check for a boolean value:
Class User < ActiveRecord::Base
def self.find_for_authentication(conditions)
super(conditions.merge(:deleted_flag => false))
end
我更喜欢时间戳 (created_at) 字段.如何检查这样的字段是否为空?数据库为以下检查抛出错误:
I would prefer a timestamp (created_at) field. How can I check whether such a field is empty? The database throws errors for the following checks:
super(conditions.merge(:deleted_at => false)) # also tried nil, "NULL" and "IS NULL"
推荐答案
你不能使用这个解决方案,当然不修改设计.Devise 会将您的条件直接发送到数据库,因此无法调用方法或使用类似 squeel 的库(这将允许类似 where{created_at == nil}
.
You can't using this solution, without modifying devise of course. Devise will send your conditions directly to the database, so no way of calling a method or using a library like squeel (that will allow something like where{created_at == nil}
.
您可以使用How to "软删除"使用 Devise 的用户,但错误消息将是:您必须先确认您的帐户,然后才能继续."
You could use the solution provided in How to "soft delete" user with Devise, but the error message will be: "You have to confirm your account before continuing."
将此添加到您的资源模型:
Add this to your resource model:
def inactive_message
!!deleted_at ? :deleted : super
end
并向您的语言环境添加一条消息:
And add a message to your locales:
en:
devise:
failure:
deleted: "Your account was deleted."
希望能帮到你!
这篇关于PostgreSQL 检查时间戳字段是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!