【问题标题】:rails include with array of id on conditionrails 在条件下包含 id 数组
【发布时间】:2015-05-04 13:25:37
【问题描述】:

我尝试使用 where 条件和包含的值来查找数组。 但是遇到一些错误..

以下效果很好,并返回受益人,包括用户

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)'

【问题讨论】:

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


【解决方案1】:

试试下面的。

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

【讨论】:

    【解决方案2】:

    你为什么不试试where 方法而不是find_all_by_id

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

    【讨论】:

      【解决方案3】:

      自己找到答案

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

      谢谢

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多