情景:
[code="Ruby"]
class Model < ActiveRecord::Base
before_create :xxoo
def xxoo
other = OtherModel.create
end
end
[/code]
请问这种情况下,如果 Model#save 失败后, OtherModel会回滚吗?
也就是说 xxoo 方法是否包含在 Model#save 的事务中.
情景:
[code="Ruby"]
class Model < ActiveRecord::Base
before_create :xxoo
def xxoo
other = OtherModel.create
end
end
[/code]
请问这种情况下,如果 Model#save 失败后, OtherModel会回滚吗?
也就是说 xxoo 方法是否包含在 Model#save 的事务中.
刚才理解错你的题目了... :lol:
[quote]
请问这种情况下,如果 Model#save 失败后, OtherModel会回滚吗?
也就是说 xxoo 方法是否包含在 Model#save 的事务中. [/quote]
这要看你的调用save是更新还是创建.
a.new,
a.save,
会调用before_create,同样产生错误也会回滚.
a=A.find(1)
a-----
a.save,
不会调用before_create,会调用before_update,产生错误也会回滚.