【问题标题】:Unknown column error in where clause when using includes使用包含时 where 子句中出现未知列错误
【发布时间】:2017-12-05 14:23:48
【问题描述】:

我有这个问题:

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::Error: 'where 子句'中的未知列 'foo.price': SELECT recipes.id AS t0_r0, recipes.foo_id AS t0_r1, recipes.parent_recipe_id AS t0_r2,recipes 987654332 @为t0_r3,recipes 987654334 @作为t0_r4,quantities 987654336 @作为t0_r5,recipes 987654338 @作为t0_r6,quantities 987654339 @ 987654340 t0_r7,foos 987654342 @为t1_r0,foos 987654344 @作为t1_r1,foos 987654346 @作为t1_r2,foos 987654347 foos 987654350 @ created_at t1_r4,foosupdated_at AS t1_r5 FROM recipes LEFT OUTER JOIN foos ON foosid = recipesfoo_id WHERE fooprice = 0.0 p >

为什么这不起作用?我正在使用其他一切正常的查询。

【问题讨论】:

  • @mudasobwa 给了我同样的错误减去 globalize gem 的东西。

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


【解决方案1】:

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

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

【讨论】:

  • eager_load(:foo).where(foo: { price: 0 })includes(:foo).where(foo: { price: 0 }).references(:foos) 给我同样的错误。
  • Uhhh.. 对不起,我不需要添加 references,因为我没有直接使用 SQL,但我错过了您已将 foo: 更改为 foos:。它现在正在工作。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 2021-12-24
  • 2012-04-25
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多