Most efficient way to get a subset of tagged objects from acts_as_taggable find_tagged_with?
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
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.
精彩评论