I have a Rails 3.2 app where I am trying to grab a group of users. My Rails code for the query looks like:
@users = User.where("user_id IN (?)", foo)
which generates the following SQL:
SELECT 'users'.* FROM 'users' WHERE id IN (1, 2, 3, 4)
When I run an explain on this query, it is searching all rows in my users table. It lists PRIMARY
under the possible_keys
field, but the key is showing as null.
If I remove the "IN (?)" part and pull just one record:
@user = User.where("user_id = ?", bar)
generating
SELECT 'users'.* FROM 'users' WHERE id = 1
then the query is executed using the primary key as the index and searching only a single row.
Any idea why this isn't working in my first example? Am I structuring this query incorrectly in Rails?