Hello

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:

1. Reload the ActiveRecord class in a spec
2. 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.

Cheers,


Peter



--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouprubyonrails-talk @
categoriesrubyonrails
postedApr 21, '12 at 9:50a
activeApr 21, '12 at 9:50a
posts1
users1
websiterubyonrails.org
irc#RubyOnRails

1 user in discussion

Peter Hicks: 1 post

People

Translate

site design / logo © 2021 Grokbase