2012-03-10 90 views
1

我需要在db中的許多記錄的整數列加上一些值。 我試圖做到這一點在 「乾淨」 的方式:加1到DB中的每個記錄的整數記錄

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

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

我得到錯誤:

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

但作品 「髒」 的方式:

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

有沒有「乾淨」的方式來做t他?

回答

2

update_all的第二個參數不是?的值,而是SQL請求的條件(可選)。

您可以用

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

謝謝你嘗試,它的作品! – Alexey 2012-03-11 07:11:28