Ruby Activerecord IN clause

前端 未结 1 496
感动是毒
感动是毒 2021-01-30 16:03

I was wondering if anyone knew how to do an \"IN\" clause in activerecord. Unfortunately, the \"IN\" clause is pretty much un-googleable so I have to post here. Basically I wa

1条回答
  •  梦毁少年i
    2021-01-30 16:24

    From §2.3.3 Subset Conditions of the Rails Guides:

    If you want to find records using the IN expression you can pass an array to the conditions hash:

    Client.where(:orders_count => [1,3,5])
    

    This code will generate SQL like this:

    SELECT * FROM clients WHERE (clients.orders_count IN (1,3,5))
    

    You can also use the arel syntax:

    Client.where(Client.arel_table[:order_count].in([1,3,5]))
    

    Will generate the same SQL

    0 讨论(0)
提交回复
热议问题