0
votes

I cannot find documentation describing what happens during each of the ActiveRecord lifecycle stages. The Guide and API list the available callbacks.

How would I know which callback is appropriate if I don't know the state of the model, or what took place earlier in the lifecycle?

For example, when does the model get persisted, and gain an id? The guide lists callbacks:

3.1 Creating an Object
before_validation
after_validation
before_save
around_save
before_create
around_create
after_create
after_save
after_commit/after_rollback

So, I'm pretty sure that the model hasn't been persisted, and doesn't have and id, before before_save is called. I would expect the model to have and id before after_save is called. Unfortunately, I have no idea where, between those 2 calls the model was persisted, and gained its id.

EDIT

Again, this is purely one example. I have updated the question to clarify: "What happens during each of the ActiveRecord model lifecycle stages?"

2
A downvote with no explanation? I'd make the question better, if I knew what you didn't like about it.Bryan Ash

2 Answers

0
votes

Actually I can't provide you a link where all this stuff is explained.

But if I were you, I'd implement a method that will be invoked on each of these callbacks, and it could help to find out, when model gaines its id.

0
votes

The "save" action is when the query to insert the record into the database is executed, and it is generally this action that will assign an id.

I can't say for sure that there aren't exceptions, but id's are usually assigned by the database during the insert process. The assigned id can be passed back to the application as part of the insert statement.