对于Mongoid 3+,是否有各种回调的图表/描述?http://mongoid.org/en/mongoid/v3/callbacks.html
例如,before_upsert和before_save之间的区别是什么。save不是由insert或update调用引起的吗?或者destroy也会调用save
另外,before_xxx和around_xxx之间有什么不同
干杯,
发布于 2014-11-14 03:45:55
在before_xxx中,代码在动作之前执行,而在around_xxx中,您可以选择在动作之前和之后执行代码。
例如,假设您想要在销毁一个用户项目(用户项目:proyects和belongs_to has_many User)后更新所有用户资产:
class ProjectsController < ApplicationController
around_destroy :destroy_belongings
def destroy_belongings
old_user = self.user
...
# Here the before_destroy ends.
yield # Here the destroy is performed itself.
# Here the after_destroy starts. It's needed to do this operation after destroy the project because, imagine, the update_belongings method calculates something related to the current number of proyects. And a simple after_destroy is not useful as we would have lost the project owner.
old_user.update_belongings
end
endhttps://stackoverflow.com/questions/25546537
复制相似问题