Created
October 14, 2010 14:51
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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