Reputation: 3299
I have the model Store
. I would like to check an existence of an entry in the database by Store.where(:google_place_id => 'XXXX')
. I just want to check if it exists in the database, regardless of whether it's (soft) deleted or not.
When I try that, rails runs this 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]]
After doing some research, I stumbled upon the unscoped
property, which would remove this deleted_at
clause from being included, but when that also gets rid of the entire WHERE clause. E.g if I tried this Store.where(:google_place_id => 'XXXX').unscoped
it runs this 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]]
Can someone clarify what I am doing wrong?
Upvotes: 2
Views: 2043
Reputation: 39313
From the paranoia
gem README:
Store.with_deleted.where(google_place_id: 'xxx')
Upvotes: 7
Reputation: 1784
First of all you can try to change it's ordering like:
Store.unscoped.where(:google_place_id => 'XXXX')
Upvotes: 1
Reputation: 564
Use the unscoped scope to ignore not deleted scope
Store.unscoped.where(:google_place_id => 'XXXX')
Upvotes: 1