Created
September 16, 2020 18:02
-
-
Save inopinatus/cca1bdc6e315e98bc0a7e7c322e3b67c to your computer and use it in GitHub Desktop.
programmatic LIKE in Arel
This file contains 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
scope :search_uid, ->(*terms) { | |
where(arel_attribute(:uid).matches_any(terms.map {|term| "%#{sanitize_sql_like term}%" }, nil, true)) | |
} |
This file contains 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
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