Skip to content

Instantly share code, notes, and snippets.

@rubiety
Created August 27, 2008 15:11
Show Gist options
  • Save rubiety/7497 to your computer and use it in GitHub Desktop.
Save rubiety/7497 to your computer and use it in GitHub Desktop.
named_scope :with_keywords, lambda {|q|
fields = %w(title information items.sku)
tokens = q.split.collect {|c| "%#{c.downcase}%"}
clause = (["(" + fields.map {|f| "#{f} LIKE ?" }.join(" OR ") + ")"] * tokens.size).join(" AND ")
{ :include => [:items], :conditions => [clause, *(tokens * fields.size).sort] }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment