请教大大,下面代码是不是写的不对,注释的地方标出了问题。
数据库是 sqlite3,框架是 rails 3.2.22,ruby 版本是 2.1.10
感谢!
destruction_item1s.delete_all
kases.each do |kase|
# DestructionItem1.transaction do
kase.kase_item1s.each do |kase_item1|
found = false
logger.debug(destruction_item1s.count) # 这一行count是有数字的 <-------------
byebug
# 但下面的这个循环并不会被执行,因为destruction_item1s = [],是空的 <-------------
destruction_item1s.each do |destruction_item1|
if kase_item1.product_id == destruction_item1.product_id
destruction_item1.amount += kase_item1.amount
found = true
end
end
if !found
DestructionItem1.create(
destruction_id: id,
category_id: kase_item1.category_id,
product_id: kase_item1.product_id,
name: kase_item1.name,
spec: kase_item1.spec,
bottles_per_box: kase_item1.bottles_per_box,
amount: kase_item1.amount,
amount_box: kase_item1.amount_box,
price_supply: kase_item1.price_supply,
price_market: kase_item1.price_market,
)
end
end
# end
end