【发布时间】:2013-03-27 10:40:22
【问题描述】:
我在控制器中有一个示例动作。
def some_action
product = Product.new
product.name = "namepro"
if product.save
client.update_attribute(:product_id,product.id)
end
end
如何为这段代码添加交易?我尝试使用此示例代码:
def some_action
**transaction do**
product = Product.new
product.name = "namepro"
if product.save
client.update_attribute(:product_create,Time.now)
end
**end**
end
但它会产生这个错误:
undefined method `transaction'
我读到过在控制器中使用事务是一种不好的做法,但我不知道原因是什么 (http://markdaggett.com/blog/2011/12/01/transactions-in-rails/)
在示例中,如果产品已创建并保存并且客户端更新失败...Rails 不能什么都不做。
谢谢。
【问题讨论】:
标签: ruby-on-rails-3 transactions controller