arel_table使いながら、たくさんの条件を配列にいれつつ検索

何を言っているんでしょう。
コード見たほうが早いですかね

1
2
3
4
5
6
7
8
9
# andで条件組み立て
def condition
  cons = []
  cons << User.arel_table[:name].matches("%hoge%")
  cons << User.arel_table[:nick_name].matches("%fuga%")
  cons.inject(&:and)
end

user.where(condition)

配列の結合で一瞬悩んだのでメモ。
andをorにすると、どれかに一致するという条件になります。

Comments