Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all Rails records from an array of IDs

I have an array of record IDs ["303", "430", "4321", "5102"]. I want to get all records that match these IDs, using SQL:

acceptable_ids = ["303", "430", "4321", "5102"]
@users = User.where("is_awesome = true AND id IN acceptable_ids)

Gives this error:

ActiveRecord::StatementInvalid (PG::SyntaxError: ERROR:  syntax error at or near "["

What is the correct way to write my query to get all users with ids that match acceptable_ids?

Note:

I am aware of User.find(acceptable_ids), but can't use this since I am constructing a SQL query with select, where, and join clauses.

like image 698
Don P Avatar asked Oct 25 '25 01:10

Don P


1 Answers

User.where(is_awesome: true, id: acceptable_ids)
like image 73
Kyle Decot Avatar answered Oct 26 '25 15:10

Kyle Decot