我是 ActiveRecord 事务的新手。在下面的代码中,第一个 update_attributes 导致警告:无法批量分配 protected 属性:account_type_cdx,这没关系。但令我惊讶的是,下一行 self.update_attributes!(:purchased => true) 被执行并存储在数据库中。我原以为它会回滚,因为第一个失败了。
我一定是遗漏了什么... 有什么提示吗?
def complete_purchase(current_user_id, plan_name)
Rails.logger.debug "COMPLETE PURCHASE"
user = User.find(current_user_id)
ActiveRecord::Base.transaction do
user.update_attributes!(:account_type_cdx => plan_name.to_sym)
self.update_attributes!(:purchased => true)
end
end
我遵循了这篇文章的建议:http://markdaggett.com/blog/2011/12/01/transactions-in-rails/
谢谢。
最佳答案
Rails 忽略那些没有在 attr_accessible
类调用中明确列出的记录(因此第一次更新警告)。它不会使事务失败,这就是为什么您要达到(并完成)第二个 update_attributes!通常。
关于ruby-on-rails-3 - ActiveRecord 事务不回滚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147708/