Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ruby on rails - What is the order of ActiveRecord callbacks and validations?

I was wondering in what order are callbacks and validations called when an ActiveRecord object is created.

Let’s say I have some custom validations & callbacks like the following:

validates :reference_code, :if => :reference_code, :on => :create
before_create :assign_reference

which one will run first? The callback needs to happen first or else the validation may fail.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

The most-up-to-date version of this list for the latest version of Rails can be found in the ActiveRecord::Callbacks documentation. The lists for Rails 4, 3 & 2 are below.

Rails 4

The most up-to-date version of this list can be found in the Rails 4 Guides.

Creating an object

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save
  • after_commit/after_rollback

Updating an object

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save
  • after_commit/after_rollback

Destroying an object

  • before_destroy
  • around_destroy
  • after_destroy
  • after_commit/after_rollback

Rails 3

The most up-to-date version of this list can be found in the Rails 3 Guides.

Creating an object

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_create
  • around_create
  • after_create
  • after_save

Updating an object

  • before_validation
  • after_validation
  • before_save
  • around_save
  • before_update
  • around_update
  • after_update
  • after_save

Destroying an object

  • before_destroy
  • around_destroy
  • after_destroy

Rails 2

The most up-to-date version of this list can be found in the Rails 2.3 Guides

Creating an object

  • before_validation
  • before_validation_on_create
  • after_validation
  • after_validation_on_create
  • before_save
  • before_create
  • INSERT operation
  • after_create
  • after_save

Updating an object

  • before_validation
  • before_validation_on_update
  • after_validation
  • after_validation_on_update
  • before_save
  • before_update
  • UPDATE operation
  • after_update
  • after_save

Destroying an object

  • before_destroy
  • DELETE operation
  • after_destroy

Since you need to first validate the reference_code, the assign_reference method can be called in the after_validation callback or any callback appearing after it in the list I provided above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...