You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I use active_record-acts_as. Did everything as described in the example. But now I want to make update_all and it does not work. Tried to use joins, but the query that is generated is of course wrong. I want to change all Products whose pens.color = 'red'. What am I doing wrong?
models:
class Pen < ActiveRecord::Base
acts_as :product
end
class Product < ActiveRecord::Base
actable
end
UPDATE "pens"
SET products.price = 1
WHERE "pens"."id" IN
(
SELECT "pens"."id"
FROM "pens"
INNER JOIN "products" ON "products"."actable_id" = "pens"."id" AND "products"."actable_type" = 'Pen'
WHERE (pens.color = 'red')
)
Of course, in the query you can see that it is impossible to make set products when update pens. But how to fix it?
How to change to the correct query:
UPDATE products as p
set price = 1
from pens
WHERE pens."id" = p.actable_id and pens.color = 'red' and p.actable_type = 'Pen'
?
The text was updated successfully, but these errors were encountered:
> Tv.update_all(name: 'Samsung').to_sql
SQL (0.4ms) UPDATE "tvs" SET "name" = 'Samsung'
PG::Error: ERROR: column "name" of relation "tvs" does not exist
LINE 1: UPDATE "tvs" SET "name" = 'Samsung'
^
So far, I guess, you need to do it 'by hand': Product.where("actable_id IN (SELECT id FROM tvs where color='red')").update_all(name: 'Samsung')
jeremyyap
pushed a commit
to Coursemology/active_record-acts_as
that referenced
this issue
Oct 23, 2018
I use active_record-acts_as. Did everything as described in the example. But now I want to make update_all and it does not work. Tried to use joins, but the query that is generated is of course wrong. I want to change all
Products
whosepens.color = 'red'
. What am I doing wrong?models:
problem place:
sql generated:
Of course, in the query you can see that it is impossible to make set products when update pens. But how to fix it?
How to change to the correct query:
?
The text was updated successfully, but these errors were encountered: