【问题标题】:PostgreSQL check whether timestamp field is emptyPostgreSQL 检查时间戳字段是否为空
【发布时间】:2012-10-17 14:38:59
【问题描述】:

我按照这些instructions 来检查用户在登录时是否被软删除。在下面的示例中,我可以检查布尔值:

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"

【问题讨论】:

  • 你试过:deleted_at =&gt; Nonenone吗?
  • 谢谢,没用,上面写着undefined local variableundefined constant

标签: ruby-on-rails postgresql


【解决方案1】:

当然,如果不修改设计,您将无法使用此解决方案。 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."

希望对你有帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-01
    • 1970-01-01
    • 2016-05-12
    • 2012-02-21
    • 2011-06-29
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    相关资源
    最近更新 更多