Is there any difference between:
这两者之间有什么区别吗?
after_create :after_create
and after_commit :after_commit_on_create, :on => :create
after_create:after_create和after_commit:after_commit_on_create,:on =>:create
Can these be used interchangeably?
这些可以互换使用吗?
2 个解决方案
#1
77
They are not interchangeable. The key difference is when the callback runs. In the case of after_create
, this will always be before the call to save
(or create
) returns.
他们不可以互换。关键的区别在于回调何时运行。对于after_create,这总是在调用save(或create)返回之前。
Rails wraps every save
inside a transaction and the before/after create callbacks run inside that transaction (a consequence of this is that if an exception is raised in an after_create the save will be rolled back). With after_commit
, your code doesn't run until after the outermost transaction was committed. This could be the transaction rails created or one created by you (for example if you wanted to make several changes inside a single transaction).
Rails将每个保存在一个事务中,并且在创建回调后,在该事务中进行回调(如果在after_create中出现异常,则将回滚该保存)。使用after_commit,代码直到提交了最外层的事务之后才运行。这可以是由您创建的事务rails,也可以是由您创建的事务(例如,如果您希望在一个事务中进行多个更改)。
At the point when after_save/create
runs, your save could still be rolled back and (by default) won't be visible to other database connections (e.g. a background task such as sidekiq). Some combination of these 2 is usually the motivation for using after_commit
.
当after_save/create运行时,您的save仍然可以回滚,并且(默认情况下)其他数据库连接无法看到您的save(例如sidekiq这样的后台任务)。这两者的某些组合通常是使用after_commit的动机。
#2
3
There is one major difference between these two with respect to associations. after_create is called as soon as an insert query is fired for the given object, and before the insert queries of the associations of the object. This means the values of the associated objects can be changed directly in after_create callbacks without update query.
这两者在联想方面有一个主要的区别。在向给定对象触发插入查询之后,以及在对象关联的插入查询之前调用after_create。这意味着可以在after_create回调中直接更改关联对象的值,而无需更新查询。
class Post < ActiveRecord::Base
has_one :post_body
after_create :change_post_body
def change_post_body
self.post_body.content = "haha"
#No need to save
end
end
#1
77
They are not interchangeable. The key difference is when the callback runs. In the case of after_create
, this will always be before the call to save
(or create
) returns.
他们不可以互换。关键的区别在于回调何时运行。对于after_create,这总是在调用save(或create)返回之前。
Rails wraps every save
inside a transaction and the before/after create callbacks run inside that transaction (a consequence of this is that if an exception is raised in an after_create the save will be rolled back). With after_commit
, your code doesn't run until after the outermost transaction was committed. This could be the transaction rails created or one created by you (for example if you wanted to make several changes inside a single transaction).
Rails将每个保存在一个事务中,并且在创建回调后,在该事务中进行回调(如果在after_create中出现异常,则将回滚该保存)。使用after_commit,代码直到提交了最外层的事务之后才运行。这可以是由您创建的事务rails,也可以是由您创建的事务(例如,如果您希望在一个事务中进行多个更改)。
At the point when after_save/create
runs, your save could still be rolled back and (by default) won't be visible to other database connections (e.g. a background task such as sidekiq). Some combination of these 2 is usually the motivation for using after_commit
.
当after_save/create运行时,您的save仍然可以回滚,并且(默认情况下)其他数据库连接无法看到您的save(例如sidekiq这样的后台任务)。这两者的某些组合通常是使用after_commit的动机。
#2
3
There is one major difference between these two with respect to associations. after_create is called as soon as an insert query is fired for the given object, and before the insert queries of the associations of the object. This means the values of the associated objects can be changed directly in after_create callbacks without update query.
这两者在联想方面有一个主要的区别。在向给定对象触发插入查询之后,以及在对象关联的插入查询之前调用after_create。这意味着可以在after_create回调中直接更改关联对象的值,而无需更新查询。
class Post < ActiveRecord::Base
has_one :post_body
after_create :change_post_body
def change_post_body
self.post_body.content = "haha"
#No need to save
end
end