I have a model that needs to have an attribute added to it to tell whether it is live or not, for example a boolean called disabled that can be set to true. I have a large codebase already and I don’t want to add in an unless disabled == false condition to every query.
Is there some sort of before filter that I can run before every query to check this attribute?
You can use ActiveRecord default_scope statement. It will concatenate the scope you define to each query done through the model.
now running this:
generates:
The full documentation for it can be found here. I also made a post about the topic in my blog. There are some nice insights about default_scope I’d recommend going through before applying it.