Skip to content

Instantly share code, notes, and snippets.

@kalleth
Last active December 10, 2015 02:58
Show Gist options
  • Save kalleth/231e7a6ed44f6f57c64f to your computer and use it in GitHub Desktop.
Save kalleth/231e7a6ed44f6f57c64f to your computer and use it in GitHub Desktop.
searching.rb
def build_search(initial_term, fields, child_fields)
term = "%#{initial_term}%"
simple = fields.nil? ? blank : build_simple_searches(term, fields)
# here, simple is []
complex = child_fields.nil? ? blank : build_complex_searches(term, child_fields)
simple | complex
end
def build_simple_searches(term, fields)
search = blank
fields.each do |field, _|
search |= @model.all(field.to_sym.like => term)
end
search
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment