LoginSignup
9
8

More than 5 years have passed since last update.

Rails4.1でActiverecordでor条件とnot条件の複合検索する

Last updated at Posted at 2015-11-20

rails4.1 なら

こんな感じ

or_confition = Portfolio.where.not(name: nil, photo_file_name: nil).where_values.reduce(:or)

Portfolio.where(or_condition)

SQL実行結果はこんな感じ

Portfolio Load (0.3ms)  SELECT `portfolios`.* FROM `portfolios` WHERE (`portfolios`.`name` IS NOT NULL OR `portfolios`.`photo_file_name` IS NOT NULL)

Rails4.2だとエラーになるようです。その場合はこのgemも使えそうです。

rails5で追加されるorをrails4.2で使うwhere-or Gem

9
8
2

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
9
8