Suthan Bala
Suthan Bala

Reputation: 3299

Rails how to get entries where we ignore deleted_at property along with other properties

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

Answers (3)

mb21
mb21

Reputation: 39313

From the paranoia gem README:

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

Upvotes: 7

AntonTkachov
AntonTkachov

Reputation: 1784

First of all you can try to change it's ordering like:

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

Upvotes: 1

a131
a131

Reputation: 564

Use the unscoped scope to ignore not deleted scope

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

Upvotes: 1

Related Questions