Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails - execution sequence of after create callback & nested attributes

I have a simple set up of User and UserProfile model with User has_one :user_profile and 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.

Case 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.

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

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?

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

like image 668
prasvin Avatar asked Mar 09 '12 09:03

prasvin


People also ask

What is the sequence of callbacks in rails?

The callback order is as following:after_validation. after_validation_on_create / after_validation_on_update. before_save. before_create.

How does callback work in Rails?

Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database.

Are rails callbacks Asynchronous?

Guideline #2: Asynchronous by defaultWhenever we add a callback, that is code that will execute before we can respond to a request. If a class defines 20 callbacks, that's 20 blocks of code that must execute before we can respond to the user. Generally, this will make requests take longer.

What is After_commit?

after_commit is a type of active record callback.


1 Answers

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 )

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:

  • https://github.com/rails/rails/blob/master/activerecord/lib/active_record/nested_attributes.rb#L256
  • https://github.com/rails/rails/blob/master/activerecord/lib/active_record/callbacks.rb#L302
  • https://github.com/rails/rails/blob/master/activemodel/lib/active_model/callbacks.rb#L98

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.

like image 161
Yves Senn Avatar answered Sep 21 '22 09:09

Yves Senn