0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

where メソッドで複数のカラムから同時に検索する方法

Posted at

開発環境

ruby 2.6.5
Ruby on Rails 5.2.5

本題

複数のカラムの情報まとめてあいまい検索したかったら

Task.where("content LIKE ? or title LIKE ?", "%#{params[:task][:search]}%", "%#{params[:task][:search]}%")

こんな感じで2つのカラムと2つの条件式をいれることで
まとめて検索できる。

SQL のプレースホルダーが ActiveRecord の where メソッドでも使える

それぞれの?の数と引数の数が一致しないとだめみたいなので、
条件が同じなら上みたいにする

参考にした記事

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?