Skip to content

Instantly share code, notes, and snippets.

@schovi
Created October 14, 2010 14:51
Show Gist options
  • Save schovi/626293 to your computer and use it in GitHub Desktop.
Save schovi/626293 to your computer and use it in GitHub Desktop.
Rails 3 scope for select model depend on tags. Include selecting more tags ( && or || for them)
scope :tagged_with, lambda { |tags, match_all = false|
tag_array = tags.is_a?(Array) ? TagList.new(tags) : TagList.from(tags)
where_operator = match_all ? :& : :|
where_conditions = tag_array.collect {|name| {:tags => :name =~ name} }
where_sql = where_conditions.inject(&where_operator)
select("DISTINCT prints.*").joins(:tags).where(where_sql)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment