使用include时where子句中的未知列错误

时间:2017-12-05 14:23:49

标签: mysql sql ruby-on-rails ruby activerecord

我有这个问题:

includes(:foo).where(foo: { price: 0 })

我是从名为Recipe的课程中调用它。

Foo的小数类型列price

>> Recipe.first.foo
=> #<Foo id: 1, price: #<BigDecimal:928b000,'0.0',9(18)>, slug: "something", created_at: "2017-12-05 07:54:29", updated_at: "2017-12-05 13:00:51">

我收到此错误:

  

Mysql2 ::错误:未知栏&#39; foo.price&#39;在&#39; where子句&#39;:SELECT recipesid AS t0_r0,recipesfoo_id AS t0_r1,recipes。{{1} } AS t0_r2,parent_recipe_idrecipes AS t0_r3,namerecipes AS t0_r4,descriptionquantities AS t0_r5,{{1} }。slug AS t0_r6,recipescreated_at AS t0_r7,quantitiesupdated_at AS t1_r0,foosid AS t1_r1,fooscategory_id AS t1_r2,foosprice AS t1_r3,foosslug AS t1_r4,fooscreated_at AS t1_r5 FROM foos LEFT OUTER JOIN updated_at ON recipesfoos = foosid WHERE {{1} }。recipes = 0.0

为什么这不起作用?我正在使用其他查询,这些查询都完美无缺。

1 个答案:

答案 0 :(得分:4)

您应该在foo:子句中将foos:(您的关联名称)替换为where(您的表名)。

includes(:foo).where(foos: { price: 0 })