Reputation: 1147
so I have the following rails query on postgres that worked fine when I ran rails 4
@products = @products.offset(offset).limit(@limit).where(status: "visible").where("add_to_inventory =? OR (add_to_inventory =? AND inventory !=? )", false, true, "{}").where.not("ARRAY(select distinct (jsonb_each(inventory)).value->>'quantity' as integer)='{0}'")
Now i recently updated to the latest stable rails 5, and now I'm getting the following error everytime this is evaluated:
PG::InvalidParameterValue: ERROR: cannot call jsonb_each on a non-object : SELECT DISTINCT "products"."priority" AS alias_0, "products"."id" AS alias_1, "products"."id" FROM "products" INNER JOIN "categories" ON "categories"."deleted" = $1 AND "categories"."id" = "products"."category_id" LEFT OUTER JOIN "product_translations" ON "product_translations"."product_id" = "products"."id" WHERE "products"."deleted" = $2 AND (categories.company_id =1) AND "products"."status" IN ($3, $4) AND "products"."category_id" IN ($5, $6, $7, $8, $9, $10, $11, $12) AND "products"."status" = $13 AND (add_to_inventory =FALSE OR (add_to_inventory =TRUE AND inventory !='{}' )) AND NOT (ARRAY(select distinct (jsonb_each(inventory)).value->>'quantity' as integer)='{0}') ORDER BY "products"."priority" ASC, "products"."id" ASC LIMIT $14 OFFSET $15
What are possible changes that caused this and how to fix it? thanks
Upvotes: 1
Views: 899
Reputation: 1147
in Rails 5 they changed this, you have to use default: {} instead of default: '{}' for jsob b migrations.
Working query:
@products = @products.offset(offset).limit(@limit).where(status: "visible").where("add_to_inventory =? OR (add_to_inventory =? AND inventory !=? )", false, true, {}).where.not("ARRAY(select distinct (jsonb_each(inventory)).value->>'quantity' as integer)='{0}'")
Upvotes: 2