views:

25

answers:

1

For example, I have a named scope sfw_only in my Image model that returns images with nsfw == false.

I'm also using acts_as_taggable_on_steroids - and I'm trying to think of the most efficient way to do something like the following.

if !params[:tag].nil?
  if nsfw_mode
    @images = Image.find_tagged_with(params[:tag])
  else
    @images = Image.find_tagged_with(params[:tag])
    ... remove images with nsfw == true
  end 
else
  if nsfw_mode
    @images = Image.all
  else
    @images = Image.sfw_only
end
+1  A: 

I'm not familiar with the acts_as_taggable_on_steroids. But the documentation leads me to believe it's compatible with named scopes.

So you should just be able to do

if !params[:tag].nil?
  if nsfw_mode
    @images = Image.find_tagged_with(params[:tag])
  else
    @images = Image.find_tagged_with(params[:tag]).sfw_only
    ... remove images with nsfw == true
  end 
else
  if nsfw_mode
    @images = Image.all
  else
    @images = Image.sfw_only
end

In the case that it doesn't you could make you're own named_scope that emulates finds_tagged_with and chain it with your sfw_only scope. This post describes how to do it, if it hasn't already been merged into the source.

EmFi
Thanks, didn't realize named scopes and finds were chainable.
mculp