Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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