I have a default scope in an ActiveRecord model which includes variables from a configuration file:
default_scope :conditions => ([ “stp_indicator = ‘C’ OR data_source = ‘VSTP’ OR (category NOT IN (?) AND atoc_code NOT IN (?))”, $CONFIG[‘RESTRICTIONS’][‘category’], $CONFIG[‘RESTRICTIONS’][‘toc’]])
There are some instances where I want to ignore the default scope (e.g. when I explicitly want to find a record with a restricted category, and I’m using .unscoped on these queries.
What I’m stuck on is how I test this using RSpec.
At the Rails console, I can use ‘reload!’ to reload models, but I can’t work out how to do this in a spec. I’ve looked at ActiveRecord::Persistence.reload (http://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-reload), but it doesn’t appear to do what I’m looking for. I’ve also tried specifying the default scope using a lambda, but this returns a Proc or an error whatever way I try - although I’ll freely admit I’m not confident on my knowledge of lambdas.
Is it possible to do one of the following:
Reload the ActiveRecord class in a spec
Dynamically evaluate the default_scope every time a query runs
My gut feeling is that I’m doing this wrong, and I should be using a named scope for all queries but the one currently unscoped - but I wanted to check in and make sure somebody with better Rails knowledge than me agrees.