1

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?

Community
  • 1
  • 1
sneglefar
  • 117
  • 9

1 Answers1

1

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.

CottonEyeJoe
  • 720
  • 1
  • 7
  • 28
  • You can also write that as `xyz.where(category_id: a)` – Dan Feb 01 '17 at 16:59
  • Thanks - that is a solution to what I asked. I ended up actually getting it reverted back into an AR with the order_as_specified gem! – sneglefar Feb 01 '17 at 17:56