Could you explain in detail what the :before_save
and :before_create
Ruby on Rails callbacks are, and what they have to do with Rails validations? Does validation occur after :before_save
or :before_create
?
相关问题
- Question marks after images and js/css files in ra
- Using :remote => true with hover event
- Eager-loading association count with Arel (Rails 3
- How to specify memcache server to Rack::Session::M
- Date with SimpleDateFormat in Java
相关文章
- Angular Material Stepper causes mat-formfield to v
- Ruby using wrong version of openssl
- Right way to deploy Rails + Puma + Postgres app to
- Why doesn't Django enforce my unique_together
- AWS S3 in rails - how to set the s3_signature_vers
- Difference between Thread#run and Thread#wakeup?
- how to call a active record named scope with a str
- How to add a JSON column in MySQL with Rails 5 Mig
before_create
vsbefore_save :on => :create
Sometimes you have to be careful of the order of the callbacks
See here for more details: http://pivotallabs.com/activerecord-callbacks-autosave-before-this-and-that-etc/In a create operation under Rails, there are six callbacks before the database operation, and two after. In order, these are:
before_validation
before_validation_on_create
after_validation
after_validation_on_create
before_save
before_create
DATABASE INSERT
after_create
after_save
Update operations have exactly the same set, except read
update
instead ofcreate
everywhere (and UPDATE instead of INSERT).From this, you can see that validation is carried out before the
before_save
andbefore_create
callbacks.The
before_save
occurs slightly before thebefore_create
. To the best of my knowledge, nothing happens between them; butbefore_save
will also fire on Update operations, whilebefore_create
will only fire on Creates.before_save
is called every time an object is saved. So for new and existing objects. (create and update action)before_create
only before creation. So only for new objects (create action)