I am trying to run a sql like below
select name from appointments where location_id in (2,3,4)
the following does not work. I am using PostgreSQL
a = [2,3,4]
Appointment.select(:name).where("location_id IN ?", a)
ActiveRecord::StatementInvalid: PGError: ERROR: syntax error at or near "2"
LINE 1: ... FROM "appointments" WHERE (location_id IN 2,3,4)
^
: SELECT name FROM "appointments" WHERE (location_id IN 2,3,4)
You can use this:
Appointment.select(:name).where(:location_id => [2,3,4])
Hope this helps
I don't know rails, but it looks to me like you need to do this:
Appointment.select(:name).where("location_id IN (?)", a)
i.e., put brackets around the ?
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With