diff options
Diffstat (limited to 'guides/source')
-rw-r--r-- | guides/source/active_record_basics.md | 6 | ||||
-rw-r--r-- | guides/source/active_record_callbacks.md | 21 | ||||
-rw-r--r-- | guides/source/active_record_querying.md | 2 | ||||
-rw-r--r-- | guides/source/active_record_validations.md | 2 | ||||
-rw-r--r-- | guides/source/getting_started.md | 4 |
5 files changed, 25 insertions, 10 deletions
diff --git a/guides/source/active_record_basics.md b/guides/source/active_record_basics.md index 4cf4111bf0..5c1de97aa8 100644 --- a/guides/source/active_record_basics.md +++ b/guides/source/active_record_basics.md @@ -309,12 +309,12 @@ user = User.find_by(name: 'David') user.destroy ``` -If you'd like to delete several records in bulk, you may use `destroy_all` -method: +If you'd like to delete several records in bulk, you may use `destroy_by` +or `destroy_all` method: ```ruby # find and delete all users named David -User.where(name: 'David').destroy_all +User.destroy_by(name: 'David') # delete all users User.destroy_all diff --git a/guides/source/active_record_callbacks.md b/guides/source/active_record_callbacks.md index ebdee446f9..614737c342 100644 --- a/guides/source/active_record_callbacks.md +++ b/guides/source/active_record_callbacks.md @@ -239,13 +239,12 @@ Skipping Callbacks Just as with validations, it is also possible to skip callbacks by using the following methods: -* `decrement` +* `decrement!` * `decrement_counter` * `delete` * `delete_all` -* `increment` +* `increment!` * `increment_counter` -* `toggle` * `update_column` * `update_columns` * `update_all` @@ -310,7 +309,7 @@ end ### Using `:if` and `:unless` with a `Proc` -Finally, it is possible to associate `:if` and `:unless` with a `Proc` object. This option is best suited when writing short validation methods, usually one-liners: +It is possible to associate `:if` and `:unless` with a `Proc` object. This option is best suited when writing short validation methods, usually one-liners: ```ruby class Order < ApplicationRecord @@ -338,6 +337,20 @@ class Comment < ApplicationRecord end ``` +### Combining Callback Conditions + +When multiple conditions define whether or not a callback should happen, an `Array` can be used. Moreover, you can apply both `:if` and `:unless` to the same callback. + +```ruby +class Comment < ApplicationRecord + after_create :send_email_to_author, + if: [Proc.new { |c| c.user.allow_send_email? }, :author_wants_emails?], + unless: Proc.new { |c| c.article.ignore_comments? } +end +``` + +The callback only runs when all the `:if` conditions and none of the `:unless` conditions are evaluated to `true`. + Callback Classes ---------------- diff --git a/guides/source/active_record_querying.md b/guides/source/active_record_querying.md index fd1dcf22c0..cb738f0657 100644 --- a/guides/source/active_record_querying.md +++ b/guides/source/active_record_querying.md @@ -623,6 +623,8 @@ To select only a subset of fields from the result set, you can specify the subse For example, to select only `viewable_by` and `locked` columns: ```ruby +Client.select(:viewable_by, :locked) +# OR Client.select("viewable_by, locked") ``` diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md index 0fda7c5cfd..0c57802188 100644 --- a/guides/source/active_record_validations.md +++ b/guides/source/active_record_validations.md @@ -934,7 +934,7 @@ end ### Using a Proc with `:if` and `:unless` -Finally, it's possible to associate `:if` and `:unless` with a `Proc` object +It is possible to associate `:if` and `:unless` with a `Proc` object which will be called. Using a `Proc` object gives you the ability to write an inline condition instead of a separate method. This option is best suited for one-liners. diff --git a/guides/source/getting_started.md b/guides/source/getting_started.md index be59cd0cfa..7ee0d8c916 100644 --- a/guides/source/getting_started.md +++ b/guides/source/getting_started.md @@ -683,7 +683,7 @@ If you look in the `db/migrate/YYYYMMDDHHMMSS_create_articles.rb` file (remember, yours will have a slightly different name), here's what you'll find: ```ruby -class CreateArticles < ActiveRecord::Migration[5.0] +class CreateArticles < ActiveRecord::Migration[6.0] def change create_table :articles do |t| t.string :title @@ -1555,7 +1555,7 @@ In addition to the model, Rails has also made a migration to create the corresponding database table: ```ruby -class CreateComments < ActiveRecord::Migration[5.0] +class CreateComments < ActiveRecord::Migration[6.0] def change create_table :comments do |t| t.string :commenter |