如何根据模型集合编写where in子句

时间:2013-11-06 00:42:21

标签: ruby-on-rails activerecord ruby-on-rails-4

我正在尝试编写这个将成为WHERE IN子句的AR查询:

User.include(:country).where(:country_id, @countries).order(:...).limit(10)

其中@countries可以是AR查询的任何结果:

@countries = Country.find(...)

我怎样才能这样做:

WHERE country_id in (1,2,3,4,5)

1 个答案:

答案 0 :(得分:1)

User.include(:country).where('country_id IN (?)', @countries.map(&:id))...