postgresql - Get all Rails records from an array of IDs -


i have array of record ids ["303", "430", "4321", "5102"]. want records match these ids, using sql:

acceptable_ids = ["303", "430", "4321", "5102"] @users = user.where("is_awesome = true , id in acceptable_ids) 

gives error:

activerecord::statementinvalid (pg::syntaxerror: error:  syntax error @ or near "[" 

what correct way write query users ids match acceptable_ids?

note:

i aware of user.find(acceptable_ids), can't use since constructing sql query select, where, , join clauses.

you this.

@users = user.where("is_awesome = true , id in (#{acceptable_ids.join(', ')})") 

i'm sure i've seen simpler way can't recall @ moment... above should work.

edit

however, can see vociferous comments not popular answer, should @ of alternatives linked , listed, e.g.

# raise exception if value not found user.find( [1,3,5] )  # not raise exception user.find_all_by_id( [1,3,5] ) 

Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -