1
1

More than 3 years have passed since last update.

【Rails】 LIKEで複数の値で検索する方法

Posted at

LIKEで複数の値で検索する方法

LIKEで複数の値で検索する方法をメモとして残します。

gem 'activerecord-like'を使えばもっと簡単にできるのですが、別の方法として実装してみました。


scope = User.all
names = ['yama', 'mae']
search_sql = []

names = names.map do |n|
    search_sql.push('name like ?')
    "%#{name}%"
end

scope = scope.where( search_sql.join(' or '), *names )
1
1
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1