通过在rails3中使用IN运算符来查找位置

Omn*_*ent 4 postgresql activerecord ruby-on-rails-3

我试图运行如下的SQL

select name from appointments where location_id in (2,3,4)
Run Code Online (Sandbox Code Playgroud)

以下不起作用.我在用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)
Run Code Online (Sandbox Code Playgroud)

JCo*_*era 5

你可以用这个:

Appointment.select(:name).where(:location_id => [2,3,4])
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助