rails包含带有id的数组条件

时间:2015-05-04 13:25:37

标签: ruby-on-rails ruby arrays ruby-on-rails-4 orm

我尝试使用where where和includes来查找值数组。 但是得到了一些错误..

以下效果很好并且包含用户的受益人

Beneficiary.includes(:user).where("beneficiaries.id = ?",304)

但是当我尝试使用ID数组时,我会收到一些错误

Beneficiary.includes(:caterer_info).where("beneficiaries.id = ?",[304,305])

ActiveRecord::StatementInvalid: Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '304,305)'

3 个答案:

答案 0 :(得分:1)

请尝试以下操作。

Beneficiary.includes(:caterer_info).where({beneficiaries: {id: [304,305]}})

答案 1 :(得分:0)

为什么不尝试使用where方法而不是find_all_by_id

Beneficiary.includes(:caterer_info).where('beneficiaries.id IN (?)', [304,305])

答案 2 :(得分:0)

自己找到了答案

   Beneficiary.includes(:user).where("beneficiaries.id IN (?)",ids)

由于