@@ -837,58 +837,57 @@ lead to invalid data.
837
837
[ ` upsert_all ` ] :
838
838
https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-upsert_all
839
839
840
- Suppressing Callbacks
841
- ---------------------
840
+ Suppressing Saving
841
+ ------------------
842
842
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.
847
848
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.
853
853
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.
858
856
859
857
``` ruby
860
858
class User < ApplicationRecord
861
- after_create :send_welcome_email
859
+ has_many :notifications
860
+
861
+ after_create :create_welcome_notification
862
862
863
- def send_welcome_email
864
- puts " Welcome email sent to #{ self .email } "
863
+ def create_welcome_notification
864
+ notifications.create( event: " sign_up " )
865
865
end
866
866
end
867
- ```
868
867
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
+ ```
871
872
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:
874
875
875
876
``` ruby
876
- User .suppress do
877
+ Notification .suppress do
877
878
User .create(
name: " Jane" ,
email: " [email protected] " )
878
879
end
879
880
```
880
881
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.
884
884
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
888
887
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.
892
891
893
892
Halting Execution
894
893
-----------------
0 commit comments