LoginSignup
13
13

More than 5 years have passed since last update.

Ransack(Rails)で複雑なAND条件やOR条件を組み合わせる方法

Last updated at Posted at 2015-02-01

Ransackで

(col1 LIKE "%hogehoge%" OR col2 LIKE "%hogehoge%") AND (col1 LIKE "%hogehoge%" OR col2 LIKE "%hogehoge%")

のようにcondition同士をANDでつなげる方法を調べていて、日本語ネタが見つからなかったのでメモ書き。

Ransackは関連テーブルをouter joinしてくれたり、エスケープしてくれたりして便利。

{
  combinator: 'and',
  groupings: {
    '0' => {'col1_or_col2_cont' => 'hogehoge'},
    '1' => {'col1_or_col2_cont' => 'hogehoge'}
  }
}

ActiveRecordってORが入ると文字列かArelでcondition組まないといけないの大変だなぁ

ActiveRecordでORメソッドチェインはやく実装されないかな〜

https://github.com/rails/rails/pull/16052
https://github.com/olivierlacan/ask/issues/4

参考

https://github.com/activerecord-hackery/ransack/issues/169
https://github.com/activerecord-hackery/ransack#grouping-queries-by-or-instead-of-and

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