Created
February 15, 2012 23:33
-
-
Save sporkd/1840047 to your computer and use it in GitHub Desktop.
Using arel matches_any
This file contains hidden or 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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👏