Skip to content

Instantly share code, notes, and snippets.

@banderson623
Last active December 20, 2015 10:58
Show Gist options
  • Save banderson623/6119173 to your computer and use it in GitHub Desktop.
Save banderson623/6119173 to your computer and use it in GitHub Desktop.
Same rails search
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