Skip to content

Instantly share code, notes, and snippets.

@remvee
Created December 24, 2009 13:11
Show Gist options
  • Save remvee/263171 to your computer and use it in GitHub Desktop.
Save remvee/263171 to your computer and use it in GitHub Desktop.
Simple all words named_scope
# 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