Rails如何获取我们忽略deleted_at属性以及其他属性的条目

时间:2017-11-28 14:08:14

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

我有模型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]]

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

3 个答案:

答案 0 :(得分:4)

来自array_push($stuarr,${$students['name']}); gem README:

paranoia

答案 1 :(得分:1)

使用未范围的范围忽略未删除的范围

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

答案 2 :(得分:1)

首先,您可以尝试更改它的排序,如:

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