Blankman
Blankman

Reputation: 267320

Fetch all rows that column_id is in the array of id's, then order by id

I am trying this, not sure if I am doing this correctly:

User.where("region_id => ?", region_ids).order("id ASC")

region_ids = [1234,234322,234324,2343,....]

Also, will this work if the region_ids is empty (not null, but empty)

I am seeing an error:

check the manual that corresponds to your MySQL server version for the right syntax to use near '=> NULL) ORDER BY id ASC' at line 1:

When I was in debugger mode, I output the region_ids and it was [].

Upvotes: 1

Views: 116

Answers (2)

Dominik Grabiec
Dominik Grabiec

Reputation: 10665

You're mixing up Ruby and SQL syntax within the string. What you want is more like ("region_id in (?)", region_ids) or ({ "region_id" => region_ids }).

Upvotes: 2

Jim Morris
Jim Morris

Reputation: 2890

You need to reverse the order of the two lines, region_ids should be set before the User.where becuase the way it is now region_ids is NULL when you call where().

Upvotes: 0

Related Questions