Alexey
Alexey

Reputation: 2386

plus 1 to integer record for each record in DB

I need plus some value to integer column for many records in db. I'm trying to do it in "clean" way:

Transaction.where("account_id = ? AND date > ?", t.account_id, t.date).
update_all("account_state = account + ?", account_offset)

or

Transaction.where("account_id = ? AND date > ?", t.account_id, t.date).
update_all("account_state += ?", account_offset)

i get error:

QLite3::ConstraintException: constraint failed: 
UPDATE "transactions" SET account_state = (account_state + ?) 
WHERE (account_id = 1 AND date > '2012-03-01') AND (-10000)

But works "dirty" way:

Transaction.where("account_id = ? AND date > ?", t.account_id, t.date).
update_all("account_state = account + #{account_offset}")

Is there any "clean" way to do this?

Upvotes: 1

Views: 746

Answers (1)

Baldrick
Baldrick

Reputation: 24350

The second parameter of update_all is not the value of the ?, but the conditions (optional) of the SQL request.

You may try with

Transaction.where("account_id = ? AND date > ?", t.account_id, t.date).update_all(["account_state = account + ?", account_offset])

Upvotes: 2

Related Questions