LoginSignup
0
0

More than 1 year has 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