【发布时间】:2011-10-27 11:27:10
【问题描述】:
我有一个 Comment 模型,它属于 Topic 模型。在 Comment 模型上,我有一个 before_create 回调
def on_create
Topic.transaction(:require_new => true) do
Topic.connection.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE')
self.topic.increment!(:comment_counter) if conditions
end
end
问题是我得到了ActiveRecord::StatementInvalid: PGError: ERROR: SET TRANSACTION ISOLATION LEVEL must be called before any query。
还有其他方法可以设置事务隔离级别吗?
【问题讨论】:
-
可以在开始交易前设置吗?
-
这实际上是我的问题 - 如何在整个模型保存事务开始之前设置隔离级别。
标签: ruby-on-rails postgresql transactions callback