Link to API docs in AR Callbacks/Validations guide

This commit is contained in:
Earlopain 2024-03-04 11:26:35 +01:00
parent db30dd6fe7
commit 17662a4876
No known key found for this signature in database
GPG Key ID: 298A57787DC6EF08
2 changed files with 89 additions and 42 deletions

@ -310,27 +310,46 @@ Skipping Callbacks
Just as with validations, it is also possible to skip callbacks by using the following methods: Just as with validations, it is also possible to skip callbacks by using the following methods:
* `decrement!` * [`decrement!`][]
* `decrement_counter` * [`decrement_counter`][]
* `delete` * [`delete`][]
* `delete_all` * [`delete_all`][]
* `delete_by` * [`delete_by`][]
* `increment!` * [`increment!`][]
* `increment_counter` * [`increment_counter`][]
* `insert` * [`insert`][]
* `insert!` * [`insert!`][]
* `insert_all` * [`insert_all`][]
* `insert_all!` * [`insert_all!`][]
* `touch_all` * [`touch_all`][]
* `update_column` * [`update_column`][]
* `update_columns` * [`update_columns`][]
* `update_all` * [`update_all`][]
* `update_counters` * [`update_counters`][]
* `upsert` * [`upsert`][]
* `upsert_all` * [`upsert_all`][]
These methods should be used with caution, however, because important business rules and application logic may be kept in callbacks. Bypassing them without understanding the potential implications may lead to invalid data. These methods should be used with caution, however, because important business rules and application logic may be kept in callbacks. Bypassing them without understanding the potential implications may lead to invalid data.
[`decrement!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-decrement-21
[`decrement_counter`]: https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-decrement_counter
[`delete`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-delete
[`delete_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_all
[`delete_by`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-delete_by
[`increment!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-increment-21
[`increment_counter`]: https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-increment_counter
[`insert`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert
[`insert!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert-21
[`insert_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all
[`insert_all!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all-21
[`touch_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-touch_all
[`update_column`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_column
[`update_columns`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_columns
[`update_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-update_all
[`update_counters`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-update_counters
[`upsert`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert
[`upsert_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert_all
Halting Execution Halting Execution
----------------- -----------------

@ -119,46 +119,74 @@ careful.
The following methods trigger validations, and will save the object to the The following methods trigger validations, and will save the object to the
database only if the object is valid: database only if the object is valid:
* `create` * [`create`][]
* `create!` * [`create!`][]
* `save` * [`save`][]
* `save!` * [`save!`][]
* `update` * [`update`][]
* `update!` * [`update!`][]
The bang versions (e.g. `save!`) raise an exception if the record is invalid. The bang versions (e.g. `save!`) raise an exception if the record is invalid.
The non-bang versions don't: `save` and `update` return `false`, and The non-bang versions don't: `save` and `update` return `false`, and
`create` returns the object. `create` returns the object.
[`create`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-create
[`create!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-create-21
[`save`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-save
[`save!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-save-21
[`update`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update
[`update!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update-21
### Skipping Validations ### Skipping Validations
The following methods skip validations, and will save the object to the The following methods skip validations, and will save the object to the
database regardless of its validity. They should be used with caution. database regardless of its validity. They should be used with caution.
* `decrement!` * [`decrement!`][]
* `decrement_counter` * [`decrement_counter`][]
* `increment!` * [`increment!`][]
* `increment_counter` * [`increment_counter`][]
* `insert` * [`insert`][]
* `insert!` * [`insert!`][]
* `insert_all` * [`insert_all`][]
* `insert_all!` * [`insert_all!`][]
* `toggle!` * [`toggle!`][]
* `touch` * [`touch`][]
* `touch_all` * [`touch_all`][]
* `update_all` * [`update_all`][]
* `update_attribute` * [`update_attribute`][]
* `update_column` * [`update_attribute!`][]
* `update_columns` * [`update_column`][]
* `update_counters` * [`update_columns`][]
* `upsert` * [`update_counters`][]
* `upsert_all` * [`upsert`][]
* [`upsert_all`][]
Note that `save` also has the ability to skip validations if passed `validate: Note that `save` also has the ability to skip validations if passed `validate:
false` as an argument. This technique should be used with caution. false` as an argument. This technique should be used with caution.
* `save(validate: false)` * `save(validate: false)`
[`decrement!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-decrement-21
[`decrement_counter`]: https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-decrement_counter
[`increment!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-increment-21
[`increment_counter`]: https://api.rubyonrails.org/classes/ActiveRecord/CounterCache/ClassMethods.html#method-i-increment_counter
[`insert`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert
[`insert!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert-21
[`insert_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all
[`insert_all!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-insert_all-21
[`toggle!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-toggle-21
[`touch`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-touch
[`touch_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-touch_all
[`update_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-update_all
[`update_attribute`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute
[`update_attribute!`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_attribute-21
[`update_column`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_column
[`update_columns`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-update_columns
[`update_counters`]: https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-update_counters
[`upsert`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert
[`upsert_all`]: https://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-upsert_all
### `valid?` and `invalid?` ### `valid?` and `invalid?`
Before saving an Active Record object, Rails runs your validations. Before saving an Active Record object, Rails runs your validations.