Rails - execution sequence of after create callback & nested attributes

时间:2021-06-28 01:19:53

I have a simple set up of User and UserProfile model with User has_one :user_profile and UserProfile belongs_to :user.

我有一个User和UserProfile模型的简单设置,其中包含User has_one:user_profile和UserProfile belongs_to:user。

But I am unable to wrap my head around how Rails defines execution order of after_create callback and accepts_nested_attributes_for defined in my model. Lets consider these two cases.

但我无法理解Rails如何定义我的模型中定义的after_create回调和accepts_nested_attributes_for的执行顺序。让我们考虑这两种情况。

Case 1:

情况1:

class User < ActiveRecord::Base
  has_one :user_profile
  accepts_nested_attributes_for :user_profile
  after_create :test_test
end

Now, if I create a user(with user_profile_attributes hash too) via the console, the after_create callback is triggered after the user and its user profile is created.

现在,如果我通过控制台创建用户(也使用user_profile_attributes哈希),则在创建用户及其用户配置文件后触发after_create回调。

Case 2: If the after_create is placed at the top,

案例2:如果将after_create放在顶部,

class User < ActiveRecord::Base
  after_create :test_test
  has_one :user_profile
  accepts_nested_attributes_for :user_profile
end

the callback is triggered after a user has been created but before creating a user profile.

在创建用户之后但在创建用户配置文件之前触发回调。

Is this the way it is expected to function. What does Rails do internally here? Is the execution sequence simply determined by the order of the code?

这是它预期运作的方式。 Rails在这里做什么?执行顺序是否仅由代码的顺序决定?

Where do I start to dig deeper into or debug this ?

我在哪里开始深入研究或调试这个?

1 个解决方案

#1


10  

The order of the declarations in your model can have an impact on the execution order of the code. This is a source for various weird things. (for example, currently callback definitions and has_and_belongs_to_many associations are order dependent: https://github.com/rails/rails/pull/8674 )

模型中声明的顺序可能会影响代码的执行顺序。这是各种怪异事物的来源。 (例如,当前回调定义和has_and_belongs_to_many关联是依赖于顺序的:https://github.com/rails/rails/pull/8674)

To debug the issue you need to browse the rails source. Since your problem has to do with execution order, callbacks and nested attributes I would start by reading up on:

要调试此问题,您需要浏览rails源。由于您的问题与执行顺序,回调和嵌套属性有关,我将从阅读开始:

This gives you the necessary background to dig deeper. You'll notice that accepts_nested_attributes_for calls into add_autosave_association_callbacks https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb#L173 This method adds an after_create callback and as far as I know callbacks are executed in order of definition.

这为您提供了深入挖掘的必要背景。您会注意到accepts_nested_attributes_for调用add_autosave_association_callbacks https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb#L173此方法添加了一个after_create回调,据我所知,回调是按照定义顺序执行。

#1


10  

The order of the declarations in your model can have an impact on the execution order of the code. This is a source for various weird things. (for example, currently callback definitions and has_and_belongs_to_many associations are order dependent: https://github.com/rails/rails/pull/8674 )

模型中声明的顺序可能会影响代码的执行顺序。这是各种怪异事物的来源。 (例如,当前回调定义和has_and_belongs_to_many关联是依赖于顺序的:https://github.com/rails/rails/pull/8674)

To debug the issue you need to browse the rails source. Since your problem has to do with execution order, callbacks and nested attributes I would start by reading up on:

要调试此问题,您需要浏览rails源。由于您的问题与执行顺序,回调和嵌套属性有关,我将从阅读开始:

This gives you the necessary background to dig deeper. You'll notice that accepts_nested_attributes_for calls into add_autosave_association_callbacks https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb#L173 This method adds an after_create callback and as far as I know callbacks are executed in order of definition.

这为您提供了深入挖掘的必要背景。您会注意到accepts_nested_attributes_for调用add_autosave_association_callbacks https://github.com/rails/rails/blob/master/activerecord/lib/active_record/autosave_association.rb#L173此方法添加了一个after_create回调,据我所知,回调是按照定义顺序执行。