Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

ruby on rails - What is the difference between `after_create` and `after_save` and when to use which?

Are after_create and after_save the same as per functionality?

I want to do an operation with the email of a user after its account creation.

I want to do that operation when it is saved in the database.

which is preferable to use: after_create or after_save?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

after_create only works once - just after the record is first created.

after_save works every time you save the object - even if you're just updating it many years later

So if you want to do this email operation only just the once (and then never again) then use after_create.

If you want to do it every time the object is saved, then do it in after_save


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...