Reputation: 10206
I've got a rails model: Product < ActiveRecord::Base
Product
has a deleted
column in the database that is a 0/1 flag.
Is there a way that I can make all queries on Product
apply the where condition WHERE deleted = 0
?
I'd like Product.all
, Product.includes
, etc... and any relation, like has_many :products
to exclude "deleted
" Products
by default.
It'd be nice to be able to override this in a one-off query some way (so I can select deleted products deliberately).
My guess is this is not possible, but I'd love to hear otherwise!
Upvotes: 0
Views: 37
Reputation: 44581
You can use default_scope
for this:
default_scope { where(deleted: 0) }
Upvotes: 2