【问题标题】:Rails how to get entries where we ignore deleted_at property along with other propertiesRails如何获取我们忽略deleted_at属性以及其他属性的条目
【发布时间】:2017-11-28 14:08:13
【问题描述】:

我有型号Store。我想通过Store.where(:google_place_id => 'XXXX') 检查数据库中是否存在条目。我只想检查它是否存在于数据库中,无论它是否(软)删除。

当我尝试这样做时,rails 会运行以下 SQL: SELECT "stores".* FROM "stores" WHERE "stores"."deleted_at" IS NULL AND "stores"."google_place_id" = $1 LIMIT $2 [["google_place_id", "XXX"], ["LIMIT", 1]]

在做了一些研究之后,我偶然发现了 unscoped 属性,它会删除这个 deleted_at 子句,但同时也会删除整个 WHERE 子句。例如,如果我尝试这个 Store.where(:google_place_id => 'XXXX').unscoped 它运行这个 SQL SELECT "stores".* FROM "stores" WHERE "stores"."deleted_at" IS NULL AND "stores"."google_place_id" = $1 LIMIT $2 [["google_place_id", "XXX"], ["LIMIT", 1]]

有人可以澄清我做错了什么吗?

【问题讨论】:

  • 您使用的是acts_as_paranoid 还是paranoia Gem?
  • 是的,我正在使用paranoia gem。

标签: ruby-on-rails activerecord rails-activerecord


【解决方案1】:

来自paranoia gem 自述文件:

Store.with_deleted.where(google_place_id: 'xxx')

【讨论】:

    【解决方案2】:

    使用无作用域忽略未删除作用域

    Store.unscoped.where(:google_place_id => 'XXXX')
    

    【讨论】:

      【解决方案3】:

      首先你可以尝试改变它的顺序:

      Store.unscoped.where(:google_place_id => 'XXXX')
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多