Created
December 24, 2009 13:11
-
-
Save remvee/263171 to your computer and use it in GitHub Desktop.
Simple all words named_scope
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
# Matches posts containing all words in the body. | |
named_scope :search, lambda { |q| | |
if connection.adapter_name == 'MySQL' | |
q = q.split(/\W/).map{|q| "[[:<:]]#{Regexp.escape(q)}[[:>:]]"} | |
{:conditions => [(['posts.body REGEXP ?'] * q.size).join(" AND "), *q]} | |
else | |
raise NotImplementedError | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment