我有以下代码段:
oldroompaxinfo = Roompaxinfo.find(:all, :conditions =>["bkgitem_id = ?",bkgitemid])
.... some code to identify which passenger ids have changed
roompaxinfo = Roompaxinfo.find(:first, :conditions => ["bkgitem_id = ? and roomno = ? and pax_id = ?", bkgitemid, roomno, oldpaxid])
roompaxinfo.pax_id = newpaxid
roompaxinfo.save
..... some code to send the changes to a remote service.
#If errors occur during send I want to re-instate the old info
if retcode.index('Error') != nil
oldroompaxinfo.each do |rp|
rp.save
end
end
问题是最后一段代码没有保存旧数据。如果某人认为由于条件检查而没有执行,那么我已经通过在flash中输出一些东西来证明它确实存在。我也试过rp.save!但这也没有帮助。
我正在使用MySQL和Ruby 1.8以及Rails 2.3.8
答案 0 :(得分:1)
当您点击循环时,Rails认为rp
不是“脏”,因此执行保存实际上并不触及数据库。如果属性发生了变化,Rails只会发出update
语句。
您可能需要保存所需的旧参数(仅pax_id
?),重新加载rp
记录,然后尝试保存:
oldroompaxinfo.each do |rp|
pax_id = rp.pax_id
rp.reload
rp.update_attributes(:pax_id => pax_id)
end