Ruby Activerecord IN Article

I was wondering if anyone knows how to do "IN" in activerecord. Unfortunately, the sentence "IN" is rather incompatible, so I have to post here. Basically, I want to answer this question: "Give me all the college students who are in these dormitories, where the id of the dormitory is in this array [id array]." I know how to write a request, given a single hostel identifier, but I don't know how to do this, given an array of identifiers.

Any help is appreciated. I am sure that this is somewhere a reposition of the question, so I will remove it as soon as the answer / best search query is found.

+59
ruby activerecord
Apr 16 2018-12-21T00:
source share
2 answers

From ยง2.3.3 Conditions for a subset of guide rails :

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

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

This code will generate SQL as follows:

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

You can also use arel syntax:

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

Will generate the same SQL

+122
Apr 16 2018-12-21T00:
source share

You can also try-

 array = [1,3,5] Client.where(orders_count: array) 
0
Apr 03 '19 at 19:32
source share



All Articles