Last active
December 20, 2015 10:58
-
-
Save banderson623/6119173 to your computer and use it in GitHub Desktop.
Same rails search
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
module ActiveRecord | |
class Base | |
# Just hack this in, since I don't have AR 4.0 installed | |
def self.where(*attrs) | |
puts attrs.inspect | |
end | |
def self.table_name | |
"erics_models" | |
end | |
end | |
end | |
class EricsModel < ActiveRecord::Base | |
SEARCHABLE_ATTRIBUTES = [:name,:id] | |
def self.search(query="") | |
params = SEARCHABLE_ATTRIBUTES.size.times.collect{|p| query} | |
EricsModel.where(SEARCHABLE_ATTRIBUTES.collect{|attr| "#{table_name}.#{attr} LIKE '%?%'" }.join(" OR "),*params) | |
end | |
end | |
EricsModel.search("Hello") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment