Skip to content

Commit 6695ac7

Browse files
authored
Merge pull request rails#52596 from codergeek121/fix-activerecord-callbacks-suppressor-section
Adapt the incorrect "Supressing Callbacks" guide section [ci skip]
2 parents d3b2fcb + 4e4d24a commit 6695ac7

File tree

1 file changed

+32
-33
lines changed

1 file changed

+32
-33
lines changed

guides/source/active_record_callbacks.md

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -837,58 +837,57 @@ lead to invalid data.
837837
[`upsert_all`]:
838838
https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-upsert_all
839839

840-
Suppressing Callbacks
841-
---------------------
840+
Suppressing Saving
841+
------------------
842842

843-
In certain scenarios, you may need to temporarily prevent certain callbacks from
844-
being executed within your Rails application. This can be useful when you want
845-
to skip specific actions during certain operations without permanently disabling
846-
the callbacks.
843+
In certain scenarios, you may need to temporarily prevent records from being
844+
saved within your callbacks.
845+
This can be useful if you have a record with complex nested associations and want
846+
to skip saving specific records during certain operations without permanently disabling
847+
the callbacks or introducing complex conditional logic.
847848

848-
Rails provides a mechanism for suppressing callbacks using the
849-
[`ActiveRecord::Suppressor`
850-
module](https://api.rubyonrails.org/classes/ActiveRecord/Suppressor.html). By
851-
using this module, you can wrap a block of code where you want to suppress
852-
callbacks, ensuring that they are not executed during that specific operation.
849+
Rails provides a mechanism to prevent saving records using the
850+
[`ActiveRecord::Suppressor` module](https://api.rubyonrails.org/classes/ActiveRecord/Suppressor.html).
851+
By using this module, you can wrap a block of code where you want to avoid
852+
saving records of a specific type that otherwise would be saved by the code block.
853853

854-
Let's consider a scenario where we have a `User` model with a callback that
855-
sends a welcome email to new users after they sign up. However, there might be
856-
cases where we want to create a user without sending the welcome email, such as
857-
during seeding the database with test data.
854+
Let's consider a scenario where a user has many notifications.
855+
Creating a `User` will automatically create a `Notification` record as well.
858856

859857
```ruby
860858
class User < ApplicationRecord
861-
after_create :send_welcome_email
859+
has_many :notifications
860+
861+
after_create :create_welcome_notification
862862

863-
def send_welcome_email
864-
puts "Welcome email sent to #{self.email}"
863+
def create_welcome_notification
864+
notifications.create(event: "sign_up")
865865
end
866866
end
867-
```
868867

869-
In this example, the `after_create` callback triggers the `send_welcome_email`
870-
method every time a new user is created.
868+
class Notification < ApplicationRecord
869+
belongs_to :user
870+
end
871+
```
871872

872-
To create a user without sending the welcome email, we can use the
873-
`ActiveRecord::Suppressor` module as follows:
873+
To create a user without creating a notification, we can use the
874+
ActiveRecord::Suppressor module as follows:
874875

875876
```ruby
876-
User.suppress do
877+
Notification.suppress do
877878
User.create(name: "Jane", email: "[email protected]")
878879
end
879880
```
880881

881-
In the above code, the `User.suppress` block ensures that the
882-
`send_welcome_email` callback is not executed during the creation of the "Jane"
883-
user, allowing us to create the user without sending the welcome email.
882+
In the above code, the `Notification.suppress` block ensures that the
883+
`Notification` is not saved during the creation of the "Jane" user.
884884

885-
WARNING: Using the Active Record Suppressor, while potentially beneficial for
886-
selectively controlling callback execution, can introduce complexity and
887-
unexpected behavior. Suppressing callbacks can obscure the intended flow of your
885+
WARNING: Using the Active Record Suppressor can introduce complexity and
886+
unexpected behavior. Suppressing saving can obscure the intended flow of your
888887
application, leading to difficulties in understanding and maintaining the
889-
codebase over time. Carefully consider the implications of suppressing
890-
callbacks, ensuring thorough documentation and thoughtful testing to mitigate
891-
risks of unintended side effects, performance issues, and test failures.
888+
codebase over time. Carefully consider the implications of using the suppressor,
889+
ensuring thorough documentation and thoughtful testing to mitigate
890+
risks of unintended side effects and test failures.
892891

893892
Halting Execution
894893
-----------------

0 commit comments

Comments
 (0)