How to query .where on array of clients?

40 Views Asked by At

Using Rails 5.

I have an ordered array of clients. It's not possible for me to get an AR out of it, and this is the only way I can get these clients.

And I want to show only the clients where a certain condition is met. .where(category_id: 1), but as this is not an AR, I can't run a .where(category_id: 1) on the array of clients.

How do I achieve this? With a Scope? But how specifically? And is this still used in Rails 5?

I am basically in the same situation as second-most-upvoted in this question and basically I have an array with my clients: ActiveRecord.find(array_of_ids), preserving order (Couldn't make the other suggestions work in the thread about turning my array into an AR)

Anyways, how can I achieve this?

1

There are 1 best solutions below

2
CottonEyeJoe On BEST ANSWER

I would iterate through the array and save the values in a variable that can be accessed by .where(category_id: )

array = [1,2,3,4,5,6,7]

array.each do |a|

  if xyz.where("category_id (?)", a) == true
   do something here
  end

end

So something like xyz.where("category_id (?)", a)should be possible now.