1

I read https://stackoverflow.com/a/13754977/1979953.

user = User.arel_table

u1 = user[:id].eq(1)
u2 = user[:id].eq(2)
u3 = user[:id].eq(3)

result = User.where(u1.or(u2).or(u3))

is ok.

How to chain like above? use each or map etc.

user = User.arel_table

foo = [
  user[:id].eq(1),
  user[:id].eq(2),
  user[:id].eq(3)
]

result = foo.map{|o| ??? }
Community
  • 1
  • 1
shingo.nakanishi
  • 2,045
  • 2
  • 21
  • 55
  • [You may also find this post helpful (non-string ways of constructing or queries on different columns)](http://stackoverflow.com/q/31096009/3444240) – potashin Nov 30 '15 at 10:38

1 Answers1

4

Use reduce:

conditions = foo.reduce(:or)
User.where(conditions)

In your case it can be just:

User.where(id: [1, 2, 3])

Code above constructs where id in (1, 2, 3) query.

potashin
  • 44,205
  • 11
  • 83
  • 107