Ruby on Rails 回调,:before_save 和:before_create 之间有什么区别? [英] Ruby on Rails Callback, what is difference between :before_save and :before_create?

查看:60
本文介绍了Ruby on Rails 回调,:before_save 和:before_create 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您能否详细解释一下 :before_save:before_create Ruby on Rails 回调是什么,以及它们与 Rails 验证有什么关系?:before_save:before_create 之后是否进行验证?

Could you explain in detail what the :before_save and :before_create Ruby on Rails callbacks are, and what they have to do with Rails validations? Does validation occur after :before_save or :before_create?

推荐答案

在 Rails 下的 create 操作中,数据库操作前有六个回调,后有两个.按顺序,这些是:

In a create operation under Rails, there are six callbacks before the database operation, and two after. In order, these are:

  1. before_validation
  2. before_validation_on_create
  3. after_validation
  4. after_validation_on_create
  5. before_save
  6. before_create

    数据库插入
  7. after_create
  8. after_save

更新操作具有完全相同的设置,除了在任何地方读取 update 而不是 create(以及 UPDATE 而不是 INSERT).

Update operations have exactly the same set, except read update instead of create everywhere (and UPDATE instead of INSERT).

由此,您可以看到验证是在 before_savebefore_create 回调之前执行的.

From this, you can see that validation is carried out before the before_save and before_create callbacks.

before_save 出现在 before_create 之前.据我所知,他们之间什么也没有发生.但是 before_save 也会在更新操作时触发,而 before_create 只会在创建时触发.

The before_save occurs slightly before the before_create. To the best of my knowledge, nothing happens between them; but before_save will also fire on Update operations, while before_create will only fire on Creates.

这篇关于Ruby on Rails 回调,:before_save 和:before_create 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆