首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mongoid3回调: before_upsert vs. before_save

Mongoid3回调: before_upsert vs. before_save
EN

Stack Overflow用户
提问于 2014-08-28 18:27:46
回答 1查看 704关注 0票数 1

对于Mongoid 3+,是否有各种回调的图表/描述?http://mongoid.org/en/mongoid/v3/callbacks.html

例如,before_upsertbefore_save之间的区别是什么。save不是由insertupdate调用引起的吗?或者destroy也会调用save

另外,before_xxxaround_xxx之间有什么不同

干杯,

EN

回答 1

Stack Overflow用户

发布于 2014-11-14 03:45:55

在before_xxx中,代码在动作之前执行,而在around_xxx中,您可以选择在动作之前和之后执行代码。

例如,假设您想要在销毁一个用户项目(用户项目:proyects和belongs_to has_many User)后更新所有用户资产:

代码语言:javascript
复制
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
end

您还可以查看相关答案herehere。此外,this other article可能对您很有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25546537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档