2012-03-10 92 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