Default find condition in ActiveRecord

I'm having the following situation:

Users can upload photos (and possibly videos, music etc. in future)
each of which must be approved by an administrator before it becomes
public. The photos are being displayed in a multitude of views
(search, best photos, user profile, random photo, recently added
photos etc.) of which each (except the photo owner's private view and
some administrative views) must only display photos that have been
approved by an administrator. That means in every of those actions, I
have to specify :conditions => 'approved is true' which is not DRY at
all, which is error prone etc.

Does anyone know of a good solution to this?

Erik

P.S. I've also thought of a solution in which unapproved/rejected
photos reside in a different table, which would also be a little
better performing for most select queries but adds complexity to the
domain model.

You could create a database view (instead of a table) with the CREATE
VIEW statement.

Todd

eallik@gmail.com wrote:

I'm having the following situation:

[...] must only display photos that have been
approved by an administrator. That means in every of those actions, I
have to specify :conditions => 'approved is true' which is not DRY at
all, which is error prone etc.

Does anyone know of a good solution to this?

My first thought is to add a method to the model:

def find_approved_photos
  return find(:all, :conditions => 'approved is true')
end

and then you can reuse Model.find_approved_photos in your controllers. Then
I remembered that rails allows you to use find_all_by_#{column_name}, which
I think might be better, and works just like find. So then:

Model.find_all_by_approved(true)

would give you all approved photos. Change to false for unapproved. Also,
you can still pass find options like:

Model.find_all_by_approved(true, :order => 'user_id', :limit => 10)

and so on. Also, if you want, you can omit the all in find_all_by_approved
(ie. find_by_approved(true)) and I think it works like find(:first). Cheers!

I realized that this is, in fact, a problem of modeling the domain:
unapproved photos aren't photos at all in the domain sense, there is
instead
an entity called PotentialPhoto or similar. I realized this when I
noticed that,
when using a boolean "approved" field in the table, PhotosController's
index isn't showing the same list of photos than what is in the
database anymore
which adds unecessary complexity.

Erik

In addition you can scope the custom find so other parameters can be
passed too (like other criteria, sort etc)

def self.find_approved(options = {})
  with_scope :find => options do
    find_all_by_approved(true, :order => 'created_at DESC')
  end
end

see http://media.railscasts.com/videos/005_using_with_scope.mov

-- linoj

Trevor, do You know with_scope method?

def self.find(*args)
  with_scope(:find => {:conditions => 'pending_approval = 0'})
{ find(*args) }
end

Regards, Jack

You might also want to look into the scope-out-rails plugin
(http://code.google.com/p/scope-out-rails/)

Adam