Rails 3 default scope, scope with override

Andrew picture Andrew · Aug 3, 2011 · Viewed 11.3k times · Source

I have a situation where the behavior of an existing app is changing and it's causing me a major headache.

My app has Photos. Photos have a status: "batch", "queue", or "complete". All the existing Photos in the app are "complete".

99% of the time I need to only show complete photos, and in all of the existing codebase I need every call to Photos to be limited to only complete photos.

However, in the screens related to uploading and classifying photos I need to be able to fairly easily override that default scope to show batched or queued photos.

Like many others, I need to find a way to easily override the default scope in certain situations. I looked at these questions (1, 2) and they don't seem to answer what I'm looking for.

The code I wish worked is this:

class Photo < ActiveRecord::Base
  ...
  default_scope where(:status=>'complete')
  scope :batch, unscoped.where(:status=>'batch')
  scope :queue, unscoped.where(:status=>'queue')
  ...
end

However, that doesn't work. I tried wrapping the scope methods in lambdas, and that didn't work either.

I realize default_scope comes with baggage, but if I can't use it with overrides then I'm looking at adding scope :complete ... and having to comb through every call to photos in my existing app and add .complete in order to filter unprocessed photos.

How would you solve this problem?

Answer

klochner picture klochner · Aug 3, 2011

def self.batch
  Photo.unscoped.where(:status=>"batch")
end
This thread is more authoritative: Overriding a Rails default_scope