Skip to content

Instantly share code, notes, and snippets.

@sporkd
Created February 15, 2012 23:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sporkd/1840047 to your computer and use it in GitHub Desktop.
Save sporkd/1840047 to your computer and use it in GitHub Desktop.
Using arel matches_any
class Question < ActiveRecord::Base
# UGLY
def self.starts_with(*strings)
# Big loop to build something like this...
where("wording LIKE ? OR wording LIKE ? OR wording LIKE ?", 'blah%', 'blaah%', 'blaaah%')
end
# PURDY
def self.starts_with(*strings)
strings = strings.map { |s| s+'%' }
where(arel_table[:wording].matches_any(strings))
end
end
@smarquezs
Copy link

👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment