Skip to content

Instantly share code, notes, and snippets.

@inopinatus
Created September 16, 2020 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save inopinatus/cca1bdc6e315e98bc0a7e7c322e3b67c to your computer and use it in GitHub Desktop.
Save inopinatus/cca1bdc6e315e98bc0a7e7c322e3b67c to your computer and use it in GitHub Desktop.
programmatic LIKE in Arel
scope :search_uid, ->(*terms) {
where(arel_attribute(:uid).matches_any(terms.map {|term| "%#{sanitize_sql_like term}%" }, nil, true))
}
scope :search, ->(params) {
params.inject(self) do |rel, (key, value)|
value.present? ? rel.where(arel_table[key].matches("%#{sanitize_sql_like(value)}%")) : rel
end
}
MyModel.search(first_name: 'foo', last_name: 'bar', ...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment