heroxav
heroxav

Reputation: 1467

Unknown column error in where clause when using includes

I have this query:

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

I am calling it from a class called Recipe.

Foo's have the decimal type column 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">

I get this error:

Mysql2::Error: Unknown column 'foo.price' in 'where clause': SELECT recipes.id AS t0_r0, recipes.foo_id AS t0_r1, recipes.parent_recipe_id AS t0_r2, recipes.name AS t0_r3, recipes.description AS t0_r4, quantities.slug AS t0_r5, recipes.created_at AS t0_r6, quantities.updated_at AS t0_r7, foos.id AS t1_r0, foos.category_id AS t1_r1, foos.price AS t1_r2, foos.slug AS t1_r3, foos.created_at AS t1_r4, foos.updated_at AS t1_r5 FROM recipes LEFT OUTER JOIN foos ON foos.id = recipes.foo_id WHERE foo.price = 0.0

Why isn't this working? I am using other queries which are all working perfectly fine.

Upvotes: 0

Views: 1509

Answers (1)

potashin
potashin

Reputation: 44581

You should replace foo: (the name of your association) with foos: (the name of your table) in the where clause.

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

Upvotes: 6

Related Questions