LoginSignup
13
10

More than 5 years have passed since last update.

ransackでのOR検索について[Rails gem]

Last updated at Posted at 2015-06-29

ransackで以下の様なOR検索をしたくなったので調べてみました。

# (a AND b) OR c
SELECT `tables`.* FROM `tables`  WHERE (((`tables`.`hoge1` = 1 AND `tables`.`hoge2` IS NOT NULL) OR `tables`.`hoge1` = 2))

OR検索の記事で見つけたのが以下
http://www.willnet.in/85

コチラを参考にransackの検索条件を作成

{
  g: {
    '0' => {
      m: 'or',
      g: {
        '0' => {
          hoge1_eq: 1,
          hoge2_not_null: 1
        },
        '1' => {
          hoge1_eq: 2
        }
      }
    }
  }
}

'0'とかがすごく気持ち悪いですが検索できるようになりました。

13
10
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
13
10