1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
|
**DO NOT READ THIS FILE ON GITHUB, GUIDES ARE PUBLISHED ON https://guides.rubyonrails.org.**
Active Record Callbacks
=======================
This guide teaches you how to hook into the life cycle of your Active Record
objects.
After reading this guide, you will know:
* When certain events occur during the life of an Active Record object.
* How to register, run, and skip callbacks that respond to these events.
* How to create relational, association, conditional, and transactional
callbacks.
* How to create objects that encapsulate common behavior for your callbacks to
be reused.
--------------------------------------------------------------------------------
The Object Life Cycle
---------------------
During the normal operation of a Rails application, objects may be [created,
updated, and
destroyed](active_record_basics.html#crud-reading-and-writing-data). Active
Record provides hooks into this object life cycle so that you can control your
application and its data.
Callbacks allow you to trigger logic before or after a change to an object's
state. They are methods that get called at certain moments of an object's life
cycle. With callbacks it is possible to write code that will run whenever an
Active Record object is initialized, created, saved, updated, deleted,
validated, or loaded from the database.
```ruby
class BirthdayCake < ApplicationRecord
after_create -> { Rails.logger.info("Congratulations, the callback has run!") }
end
```
```irb
irb> BirthdayCake.create
Congratulations, the callback has run!
```
As you will see, there are many life cycle events and multiple options to hook
into these — either before, after, or even around them.
Callback Registration
---------------------
To use the available callbacks, you need to implement and register them.
Implementation can be done in a multitude of ways like using ordinary methods,
blocks and procs, or defining custom callback objects using classes or modules.
Let's go through each of these implementation techniques.
You can register the callbacks with a **macro-style class method that calls an
ordinary method** for implementation.
```ruby
class User < ApplicationRecord
validates :username, :email, presence: true
before_validation :ensure_username_has_value
private
def ensure_username_has_value
if username.blank?
self.username = email
end
end
end
```
The **macro-style class methods can also receive a block**. Consider using this
style if the code inside your block is so short that it fits in a single line:
```ruby
class User < ApplicationRecord
validates :username, :email, presence: true
before_validation do
self.username = email if username.blank?
end
end
```
Alternatively, you can **pass a proc to the callback** to be triggered.
```ruby
class User < ApplicationRecord
validates :username, :email, presence: true
before_validation ->(user) { user.username = user.email if user.username.blank? }
end
```
Lastly, you can define [**a custom callback object**](#callback-objects), as
shown below. We will cover these later in more detail.
```ruby
class User < ApplicationRecord
validates :username, :email, presence: true
before_validation AddUsername
end
class AddUsername
def self.before_validation(record)
if record.username.blank?
record.username = record.email
end
end
end
```
### Registering Callbacks to Fire on Life Cycle Events
Callbacks can also be registered to only fire on certain life cycle events, this
can be done using the `:on` option and allows complete control over when and in
what context your callbacks are triggered.
NOTE: A context is like a category or a scenario in which you want certain
validations to apply. When you validate an ActiveRecord model, you can specify a
context to group validations. This allows you to have different sets of
validations that apply in different situations. In Rails, there are certain
default contexts for validations like :create, :update, and :save.
```ruby
class User < ApplicationRecord
validates :username, :email, presence: true
before_validation :ensure_username_has_value, on: :create
# :on takes an array as well
after_validation :set_location, on: [ :create, :update ]
private
def ensure_username_has_value
if username.blank?
self.username = email
end
end
def set_location
self.location = LocationService.query(self)
end
end
```
NOTE: It is considered good practice to declare callback methods as private. If
left public, they can be called from outside of the model and violate the
principle of object encapsulation.
WARNING. Refrain from using methods like `update`, `save`, or any other methods
that cause side effects on the object within your callback methods. <br><br>
For instance, avoid calling `update(attribute: "value")` inside a callback. This
practice can modify the model's state and potentially lead to unforeseen side
effects during commit. <br><br> Instead, you can assign values directly (e.g.,
`self.attribute = "value"`) in `before_create`, `before_update`, or earlier
callbacks for a safer approach.
Available Callbacks
-------------------
Here is a list with all the available Active Record callbacks, listed **in the
order in which they will get called** during the respective operations:
### Creating an Object
* [`before_validation`][]
* [`after_validation`][]
* [`before_save`][]
* [`around_save`][]
* [`before_create`][]
* [`around_create`][]
* [`after_create`][]
* [`after_save`][]
* [`after_commit`][] / [`after_rollback`][]
See the [`after_commit` / `after_rollback`
section](active_record_callbacks.html#after-commit-and-after-rollback) for
examples using these two callbacks.
[`after_create`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_create
[`after_commit`]:
https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_commit
[`after_rollback`]:
https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_rollback
[`after_save`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_save
[`after_validation`]:
https://api.rubyonrails.org/classes/ActiveModel/Validations/Callbacks/ClassMethods.html#method-i-after_validation
[`around_create`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-around_create
[`around_save`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-around_save
[`before_create`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-before_create
[`before_save`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-before_save
[`before_validation`]:
https://api.rubyonrails.org/classes/ActiveModel/Validations/Callbacks/ClassMethods.html#method-i-before_validation
There are examples below that show how to use these callbacks. We've grouped
them by the operation they are associated with, and lastly show how they can be
used in combination.
#### Validation Callbacks
Validation callbacks are triggered whenever the record is validated directly via
the
[`valid?`](https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-valid-3F)
( or its alias
[`validate`](https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-validate))
or
[`invalid?`](https://api.rubyonrails.org/classes/ActiveModel/Validations.html#method-i-invalid-3F)
method, or indirectly via `create`, `update`, or `save`. They are called before
and after the validation phase.
```ruby
class User < ApplicationRecord
validates :name, presence: true
before_validation :titleize_name
after_validation :log_errors
private
def titleize_name
self.name = name.downcase.titleize if name.present?
Rails.logger.info("Name titleized to #{name}")
end
def log_errors
if errors.any?
Rails.logger.error("Validation failed: #{errors.full_messages.join(', ')}")
end
end
end
```
```irb
irb> user = User.new(name: "", email: "john.doe@example.com", password: "abc123456")
=> #<User id: nil, email: "john.doe@example.com", created_at: nil, updated_at: nil, name: "">
irb> user.valid?
Name titleized to
Validation failed: Name can't be blank
=> false
```
#### Save Callbacks
Save callbacks are triggered whenever the record is persisted (i.e. "saved") to
the underlying database, via the `create`, `update`, or `save` methods. They are
called before, after, and around the object is saved.
```ruby
class User < ApplicationRecord
before_save :hash_password
around_save :log_saving
after_save :update_cache
private
def hash_password
self.password_digest = BCrypt::Password.create(password)
Rails.logger.info("Password hashed for user with email: #{email}")
end
def log_saving
Rails.logger.info("Saving user with email: #{email}")
yield
Rails.logger.info("User saved with email: #{email}")
end
def update_cache
Rails.cache.write(["user_data", self], attributes)
Rails.logger.info("Update Cache")
end
end
```
```irb
irb> user = User.create(name: "Jane Doe", password: "password", email: "jane.doe@example.com")
Password encrypted for user with email: jane.doe@example.com
Saving user with email: jane.doe@example.com
User saved with email: jane.doe@example.com
Update Cache
=> #<User id: 1, email: "jane.doe@example.com", created_at: "2024-03-20 16:02:43.685500000 +0000", updated_at: "2024-03-20 16:02:43.685500000 +0000", name: "Jane Doe">
```
#### Create Callbacks
Create callbacks are triggered whenever the record is persisted (i.e. "saved")
to the underlying database **for the first time** — in other words, when we're
saving a new record, via the `create` or `save` methods. They are called before,
after and around the object is created.
```ruby
class User < ApplicationRecord
before_create :set_default_role
around_create :log_creation
after_create :send_welcome_email
private
def set_default_role
self.role = "user"
Rails.logger.info("User role set to default: user")
end
def log_creation
Rails.logger.info("Creating user with email: #{email}")
yield
Rails.logger.info("User created with email: #{email}")
end
def send_welcome_email
UserMailer.welcome_email(self).deliver_later
Rails.logger.info("User welcome email sent to: #{email}")
end
end
```
```irb
irb> user = User.create(name: "John Doe", email: "john.doe@example.com")
User role set to default: user
Creating user with email: john.doe@example.com
User created with email: john.doe@example.com
User welcome email sent to: john.doe@example.com
=> #<User id: 10, email: "john.doe@example.com", created_at: "2024-03-20 16:19:52.405195000 +0000", updated_at: "2024-03-20 16:19:52.405195000 +0000", name: "John Doe">
```
### Updating an Object
Update callbacks are triggered whenever an **existing** record is persisted
(i.e. "saved") to the underlying database. They are called before, after and
around the object is updated.
* [`before_validation`][]
* [`after_validation`][]
* [`before_save`][]
* [`around_save`][]
* [`before_update`][]
* [`around_update`][]
* [`after_update`][]
* [`after_save`][]
* [`after_commit`][] / [`after_rollback`][]
[`after_update`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_update
[`around_update`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-around_update
[`before_update`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-before_update
WARNING: The `after_save` callback is triggered on both create and update
operations. However, it consistently executes after the more specific callbacks
`after_create` and `after_update`, regardless of the sequence in which the macro
calls were made. Similarly, before and around save callbacks follow the same
rule: `before_save` runs before create/update, and `around_save` runs around
create/update operations. It's important to note that save callbacks will always
run before/around/after the more specific create/update callbacks.
We've already covered [validation](#validation-callbacks) and
[save](#save-callbacks) callbacks. See the [`after_commit` /
`after_rollback` section](#after-commit-and-after-rollback) for examples using
these two callbacks.
#### Update Callbacks
```ruby
class User < ApplicationRecord
before_update :check_role_change
around_update :log_updating
after_update :send_update_email
private
def check_role_change
if role_changed?
Rails.logger.info("User role changed to #{role}")
end
end
def log_updating
Rails.logger.info("Updating user with email: #{email}")
yield
Rails.logger.info("User updated with email: #{email}")
end
def send_update_email
UserMailer.update_email(self).deliver_later
Rails.logger.info("Update email sent to: #{email}")
end
end
```
```irb
irb> user = User.find(1)
=> #<User id: 1, email: "john.doe@example.com", created_at: "2024-03-20 16:19:52.405195000 +0000", updated_at: "2024-03-20 16:19:52.405195000 +0000", name: "John Doe", role: "user" >
irb> user.update(role: "admin")
User role changed to admin
Updating user with email: john.doe@example.com
User updated with email: john.doe@example.com
Update email sent to: john.doe@example.com
```
#### Using a Combination of Callbacks
Often, you will need to use a combination of callbacks to achieve the desired
behavior. For example, you may want to send a confirmation email after a user is
created, but only if the user is new and not being updated. When a user is
updated, you may want to notify an admin if critical information is changed. In
this case, you can use `after_create` and `after_update` callbacks together.
```ruby
class User < ApplicationRecord
after_create :send_confirmation_email
after_update :notify_admin_if_critical_info_updated
private
def send_confirmation_email
UserMailer.confirmation_email(self).deliver_later
Rails.logger.info("Confirmation email sent to: #{email}")
end
def notify_admin_if_critical_info_updated
if saved_change_to_email? || saved_change_to_phone_number?
AdminMailer.user_critical_info_updated(self).deliver_later
Rails.logger.info("Notification sent to admin about critical info update for: #{email}")
end
end
end
```
```irb
irb> user = User.create(name: "John Doe", email: "john.doe@example.com")
Confirmation email sent to: john.doe@example.com
=> #<User id: 1, email: "john.doe@example.com", ...>
irb> user.update(email: "john.doe.new@example.com")
Notification sent to admin about critical info update for: john.doe.new@example.com
=> true
```
### Destroying an Object
Destroy callbacks are triggered whenever a record is destroyed, but ignored when
a record is deleted. They are called before, after and around the object is
destroyed.
* [`before_destroy`][]
* [`around_destroy`][]
* [`after_destroy`][]
* [`after_commit`][] / [`after_rollback`][]
[`after_destroy`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_destroy
[`around_destroy`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-around_destroy
[`before_destroy`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-before_destroy
Find [examples for using `after_commit` /
`after_rollback`](#after-commit-and-after-rollback).
#### Destroy Callbacks
```ruby
class User < ApplicationRecord
before_destroy :check_admin_count
around_destroy :log_destroy_operation
after_destroy :notify_users
private
def check_admin_count
if admin? && User.where(role: "admin").count == 1
throw :abort
end
Rails.logger.info("Checked the admin count")
end
def log_destroy_operation
Rails.logger.info("About to destroy user with ID #{id}")
yield
Rails.logger.info("User with ID #{id} destroyed successfully")
end
def notify_users
UserMailer.deletion_email(self).deliver_later
Rails.logger.info("Notification sent to other users about user deletion")
end
end
```
```irb
irb> user = User.find(1)
=> #<User id: 1, email: "john.doe@example.com", created_at: "2024-03-20 16:19:52.405195000 +0000", updated_at: "2024-03-20 16:19:52.405195000 +0000", name: "John Doe", role: "admin">
irb> user.destroy
Checked the admin count
About to destroy user with ID 1
User with ID 1 destroyed successfully
Notification sent to other users about user deletion
```
### `after_initialize` and `after_find`
Whenever an Active Record object is instantiated, either by directly using `new`
or when a record is loaded from the database, the [`after_initialize`][]
callback will be called. It can be useful to avoid the need to directly override
your Active Record `initialize` method.
When loading a record from the database the [`after_find`][] callback will be
called. `after_find` is called before `after_initialize` if both are defined.
NOTE: The `after_initialize` and `after_find` callbacks have no `before_*`
counterparts.
They can be registered just like the other Active Record callbacks.
```ruby
class User < ApplicationRecord
after_initialize do |user|
Rails.logger.info("You have initialized an object!")
end
after_find do |user|
Rails.logger.info("You have found an object!")
end
end
```
```irb
irb> User.new
You have initialized an object!
=> #<User id: nil>
irb> User.first
You have found an object!
You have initialized an object!
=> #<User id: 1>
```
[`after_find`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_find
[`after_initialize`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_initialize
### `after_touch`
The [`after_touch`][] callback will be called whenever an Active Record object
is touched. You can [read more about `touch` in the API
docs](https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-touch).
```ruby
class User < ApplicationRecord
after_touch do |user|
Rails.logger.info("You have touched an object")
end
end
```
```irb
irb> user = User.create(name: "Kuldeep")
=> #<User id: 1, name: "Kuldeep", created_at: "2013-11-25 12:17:49", updated_at: "2013-11-25 12:17:49">
irb> user.touch
You have touched an object
=> true
```
It can be used along with `belongs_to`:
```ruby
class Book < ApplicationRecord
belongs_to :library, touch: true
after_touch do
Rails.logger.info("A Book was touched")
end
end
class Library < ApplicationRecord
has_many :books
after_touch :log_when_books_or_library_touched
private
def log_when_books_or_library_touched
Rails.logger.info("Book/Library was touched")
end
end
```
```irb
irb> book = Book.last
=> #<Book id: 1, library_id: 1, created_at: "2013-11-25 17:04:22", updated_at: "2013-11-25 17:05:05">
irb> book.touch # triggers book.library.touch
A Book was touched
Book/Library was touched
=> true
```
[`after_touch`]:
https://api.rubyonrails.org/classes/ActiveRecord/Callbacks/ClassMethods.html#method-i-after_touch
Running Callbacks
-----------------
The following methods trigger callbacks:
* `create`
* `create!`
* `destroy`
* `destroy!`
* `destroy_all`
* `destroy_by`
* `save`
* `save!`
* `save(validate: false)`
* `save!(validate: false)`
* `toggle!`
* `touch`
* `update_attribute`
* `update_attribute!`
* `update`
* `update!`
* `valid?`
* `validate`
Additionally, the `after_find` callback is triggered by the following finder
methods:
* `all`
* `first`
* `find`
* `find_by`
* `find_by!`
* `find_by_*`
* `find_by_*!`
* `find_by_sql`
* `last`
* `sole`
* `take`
The `after_initialize` callback is triggered every time a new object of the
class is initialized.
NOTE: The `find_by_*` and `find_by_*!` methods are dynamic finders generated
automatically for every attribute. Learn more about them in the [Dynamic finders
section](active_record_querying.html#dynamic-finders).
Conditional Callbacks
---------------------
As with [validations](active_record_validations.html), we can also make the
calling of a callback method conditional on the satisfaction of a given
predicate. We can do this using the `:if` and `:unless` options, which can take
a symbol, a `Proc` or an `Array`.
You may use the `:if` option when you want to specify under which conditions the
callback **should** be called. If you want to specify the conditions under which
the callback **should not** be called, then you may use the `:unless` option.
### Using `:if` and `:unless` with a `Symbol`
You can associate the `:if` and `:unless` options with a symbol corresponding to
the name of a predicate method that will get called right before the callback.
When using the `:if` option, the callback **won't** be executed if the predicate
method returns **false**; when using the `:unless` option, the callback
**won't** be executed if the predicate method returns **true**. This is the most
common option.
```ruby
class Order < ApplicationRecord
before_save :normalize_card_number, if: :paid_with_card?
end
```
Using this form of registration it is also possible to register several
different predicates that should be called to check if the callback should be
executed. We will cover this in the [Multiple Callback Conditions
section](#multiple-callback-conditions).
### Using `:if` and `:unless` with a `Proc`
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
before_save :normalize_card_number,
if: ->(order) { order.paid_with_card? }
end
```
Since the proc is evaluated in the context of the object, it is also possible to
write this as:
```ruby
class Order < ApplicationRecord
before_save :normalize_card_number, if: -> { paid_with_card? }
end
```
### Multiple Callback Conditions
The `:if` and `:unless` options also accept an array of procs or method names as
symbols:
```ruby
class Comment < ApplicationRecord
before_save :filter_content,
if: [:subject_to_parental_control?, :untrusted_author?]
end
```
You can easily include a proc in the list of conditions:
```ruby
class Comment < ApplicationRecord
before_save :filter_content,
if: [:subject_to_parental_control?, -> { untrusted_author? }]
end
```
### Using Both `:if` and `:unless`
Callbacks can mix both `:if` and `:unless` in the same declaration:
```ruby
class Comment < ApplicationRecord
before_save :filter_content,
if: -> { forum.parental_control? },
unless: -> { author.trusted? }
end
```
The callback only runs when all the `:if` conditions and none of the `:unless`
conditions are evaluated to `true`.
Skipping Callbacks
------------------
Just as with [validations](active_record_validations.html), it is also possible
to skip callbacks by using the following methods:
* [`decrement!`][]
* [`decrement_counter`][]
* [`delete`][]
* [`delete_all`][]
* [`delete_by`][]
* [`increment!`][]
* [`increment_counter`][]
* [`insert`][]
* [`insert!`][]
* [`insert_all`][]
* [`insert_all!`][]
* [`touch_all`][]
* [`update_column`][]
* [`update_columns`][]
* [`update_all`][]
* [`update_counters`][]
* [`upsert`][]
* [`upsert_all`][]
Let's consider a `User` model where the `before_save` callback logs any changes
to the user's email address:
```ruby
class User < ApplicationRecord
before_save :log_email_change
private
def log_email_change
if email_changed?
Rails.logger.info("Email changed from #{email_was} to #{email}")
end
end
end
```
Now, suppose there's a scenario where you want to update the user's email
address without triggering the `before_save` callback to log the email change.
You can use the `update_columns` method for this purpose:
```irb
irb> user = User.find(1)
irb> user.update_columns(email: 'new_email@example.com')
```
The above will update the user's email address without triggering the
`before_save` callback.
WARNING. These methods should be used with caution because there may be
important business rules and application logic in callbacks that you do not want
to bypass. 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/Relation.html#method-i-insert
[`insert!`]:
https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-insert-21
[`insert_all`]:
https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-insert_all
[`insert_all!`]:
https://api.rubyonrails.org/classes/ActiveRecord/Relation.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/Relation.html#method-i-upsert
[`upsert_all`]:
https://api.rubyonrails.org/classes/ActiveRecord/Relation.html#method-i-upsert_all
Suppressing Callbacks
---------------------
In certain scenarios, you may need to temporarily prevent certain callbacks from
being executed within your Rails application. This can be useful when you want
to skip specific actions during certain operations without permanently disabling
the callbacks.
Rails provides a mechanism for suppressing callbacks using the
[`ActiveRecord::Suppressor`
module](https://api.rubyonrails.org/classes/ActiveRecord/Suppressor.html). By
using this module, you can wrap a block of code where you want to suppress
callbacks, ensuring that they are not executed during that specific operation.
Let's consider a scenario where we have a `User` model with a callback that
sends a welcome email to new users after they sign up. However, there might be
cases where we want to create a user without sending the welcome email, such as
during seeding the database with test data.
```ruby
class User < ApplicationRecord
after_create :send_welcome_email
def send_welcome_email
puts "Welcome email sent to #{self.email}"
end
end
```
In this example, the `after_create` callback triggers the `send_welcome_email`
method every time a new user is created.
To create a user without sending the welcome email, we can use the
`ActiveRecord::Suppressor` module as follows:
```ruby
User.suppress do
User.create(name: "Jane", email: "jane@example.com")
end
```
In the above code, the `User.suppress` block ensures that the
`send_welcome_email` callback is not executed during the creation of the "Jane"
user, allowing us to create the user without sending the welcome email.
WARNING: Using the Active Record Suppressor, while potentially beneficial for
selectively controlling callback execution, can introduce complexity and
unexpected behavior. Suppressing callbacks can obscure the intended flow of your
application, leading to difficulties in understanding and maintaining the
codebase over time. Carefully consider the implications of suppressing
callbacks, ensuring thorough documentation and thoughtful testing to mitigate
risks of unintended side effects, performance issues, and test failures.
Halting Execution
-----------------
As you start registering new callbacks for your models, they will be queued for
execution. This queue will include all of your model's validations, the
registered callbacks, and the database operation to be executed.
The whole callback chain is wrapped in a transaction. If any callback raises an
exception, the execution chain gets halted and a **rollback** is issued, and the
error will be re-raised.
```ruby
class Product < ActiveRecord::Base
before_validation do
raise "Price can't be negative" if total_price < 0
end
end
Product.create # raises "Price can't be negative"
```
This unexpectedly breaks code that does not expect methods like `create` and
`save` to raise exceptions.
NOTE: If an exception occurs during the callback chain, Rails will re-raise it
unless it is an `ActiveRecord::Rollback` or `ActiveRecord::RecordInvalid`
exception. Instead, you should use `throw :abort` to intentionally halt the
chain. If any callback throws `:abort`, the process will be aborted and `create`
will return false.
```ruby
class Product < ActiveRecord::Base
before_validation do
throw :abort if total_price < 0
end
end
Product.create # => false
```
However, it will raise an `ActiveRecord::RecordNotSaved` when calling `create!`.
This exception indicates that the record was not saved due to the callback's
interruption.
```ruby
User.create! # => raises an ActiveRecord::RecordNotSaved
```
When `throw :abort` is called in any destroy callback, `destroy` will return
false:
```ruby
class User < ActiveRecord::Base
before_destroy do
throw :abort if still_active?
end
end
User.first.destroy # => false
```
However, it will raise an `ActiveRecord::RecordNotDestroyed` when calling
`destroy!`.
```ruby
User.first.destroy! # => raises an ActiveRecord::RecordNotDestroyed
```
Association Callbacks
---------------------
Association callbacks are similar to normal callbacks, but they are triggered by
events in the life cycle of the associated collection. There are four available
association callbacks:
* `before_add`
* `after_add`
* `before_remove`
* `after_remove`
You can define association callbacks by adding options to the association.
Suppose you have an example where an author can have many books. However, before
adding a book to the authors collection, you want to ensure that the author has
not reached their book limit. You can do this by adding a `before_add` callback
to check the limit.
```ruby
class Author < ApplicationRecord
has_many :books, before_add: :check_limit
private
def check_limit(_book)
if books.count >= 5
errors.add(:base, "Cannot add more than 5 books for this author")
throw(:abort)
end
end
end
```
If a `before_add` callback throws `:abort`, the object does not get added to the
collection.
At times you may want to perform multiple actions on the associated object. In
this case, you can stack callbacks on a single event by passing them as an
array. Additionally, Rails passes the object being added or removed to the
callback for you to use.
```ruby
class Author < ApplicationRecord
has_many :books, before_add: [:check_limit, :calculate_shipping_charges]
def check_limit(_book)
if books.count >= 5
errors.add(:base, "Cannot add more than 5 books for this author")
throw(:abort)
end
end
def calculate_shipping_charges(book)
weight_in_pounds = book.weight_in_pounds || 1
shipping_charges = weight_in_pounds * 2
shipping_charges
end
end
```
Similarly, if a `before_remove` callback throws `:abort`, the object does not
get removed from the collection.
NOTE: These callbacks are called only when the associated objects are added or
removed through the association collection.
```ruby
# Triggers `before_add` callback
author.books << book
author.books = [book, book2]
# Does not trigger the `before_add` callback
book.update(author_id: 1)
```
Cascading Association Callbacks
-------------------------------
Callbacks can be performed when associated objects are changed. They work
through the model associations whereby life cycle events can cascade on
associations and fire callbacks.
Suppose an example where a user has many articles. A user's articles should be
destroyed if the user is destroyed. Let's add an `after_destroy` callback to the
`User` model by way of its association to the `Article` model:
```ruby
class User < ApplicationRecord
has_many :articles, dependent: :destroy
end
class Article < ApplicationRecord
after_destroy :log_destroy_action
def log_destroy_action
Rails.logger.info("Article destroyed")
end
end
```
```irb
irb> user = User.first
=> #<User id: 1>
irb> user.articles.create!
=> #<Article id: 1, user_id: 1>
irb> user.destroy
Article destroyed
=> #<User id: 1>
```
WARNING: When using a `before_destroy` callback, it should be placed before
`dependent: :destroy` associations (or use the `prepend: true` option), to
ensure they execute before the records are deleted by `dependent: :destroy`.
Transaction Callbacks
---------------------
### `after_commit` and `after_rollback`
Two additional callbacks are triggered by the completion of a database
transaction: [`after_commit`][] and [`after_rollback`][]. These callbacks are
very similar to the `after_save` callback except that they don't execute until
after database changes have either been committed or rolled back. They are most
useful when your Active Record models need to interact with external systems
that are not part of the database transaction.
Consider a `PictureFile` model that needs to delete a file after the
corresponding record is destroyed.
```ruby
class PictureFile < ApplicationRecord
after_destroy :delete_picture_file_from_disk
def delete_picture_file_from_disk
if File.exist?(filepath)
File.delete(filepath)
end
end
end
```
If anything raises an exception after the `after_destroy` callback is called and
the transaction rolls back, then the file will have been deleted and the model
will be left in an inconsistent state. For example, suppose that
`picture_file_2` in the code below is not valid and the `save!` method raises an
error.
```ruby
PictureFile.transaction do
picture_file_1.destroy
picture_file_2.save!
end
```
By using the `after_commit` callback we can account for this case.
```ruby
class PictureFile < ApplicationRecord
after_commit :delete_picture_file_from_disk, on: :destroy
def delete_picture_file_from_disk
if File.exist?(filepath)
File.delete(filepath)
end
end
end
```
NOTE: The `:on` option specifies when a callback will be fired. If you don't
supply the `:on` option the callback will fire for every life cycle event. [Read
more about `:on`](#registering-callbacks-to-fire-on-life-cycle-events).
When a transaction completes, the `after_commit` or `after_rollback` callbacks
are called for all models created, updated, or destroyed within that
transaction. However, if an exception is raised within one of these callbacks,
the exception will bubble up and any remaining `after_commit` or
`after_rollback` methods will _not_ be executed.
```ruby
class User < ActiveRecord::Base
after_commit { raise "Intentional Error" }
after_commit {
# This won't get called because the previous after_commit raises an exception
Rails.logger.info("This will not be logged")
}
end
```
WARNING. If your callback code raises an exception, you'll need to rescue it and
handle it within the callback in order to allow other callbacks to run.
`after_commit` makes very different guarantees than `after_save`,
`after_update`, and `after_destroy`. For example, if an exception occurs in an
`after_save` the transaction will be rolled back and the data will not be
persisted.
```ruby
class User < ActiveRecord::Base
after_save do
# If this fails the user won't be saved.
EventLog.create!(event: "user_saved")
end
end
```
However, during `after_commit` the data was already persisted to the database,
and thus any exception won't roll anything back anymore.
```ruby
class User < ActiveRecord::Base
after_commit do
# If this fails the user was already saved.
EventLog.create!(event: "user_saved")
end
end
```
The code executed within `after_commit` or `after_rollback` callbacks is itself
not enclosed within a transaction.
In the context of a single transaction, if you represent the same record in the
database, there's a crucial behavior in the `after_commit` and `after_rollback`
callbacks to note. These callbacks are triggered only for the first object of
the specific record that changes within the transaction. Other loaded objects,
despite representing the same database record, will not have their respective
`after_commit` or `after_rollback` callbacks triggered.
```ruby
class User < ApplicationRecord
after_commit :log_user_saved_to_db, on: :update
private
def log_user_saved_to_db
Rails.logger.info("User was saved to database")
end
end
```
```irb
irb> user = User.create
irb> User.transaction { user.save; user.save }
# User was saved to database
```
WARNING: This nuanced behavior is particularly impactful in scenarios where you
expect independent callback execution for each object associated with the same
database record. It can influence the flow and predictability of callback
sequences, leading to potential inconsistencies in application logic following
the transaction.
### Aliases for `after_commit`
Using the `after_commit` callback only on create, update, or delete is common.
Sometimes you may also want to use a single callback for both `create` and
`update`. Here are some common aliases for these operations:
* [`after_destroy_commit`][]
* [`after_create_commit`][]
* [`after_update_commit`][]
* [`after_save_commit`][]
Let's go through some examples:
Instead of using `after_commit` with the `on` option for a destroy like below:
```ruby
class PictureFile < ApplicationRecord
after_commit :delete_picture_file_from_disk, on: :destroy
def delete_picture_file_from_disk
if File.exist?(filepath)
File.delete(filepath)
end
end
end
```
You can instead use the `after_destroy_commit`.
```ruby
class PictureFile < ApplicationRecord
after_destroy_commit :delete_picture_file_from_disk
def delete_picture_file_from_disk
if File.exist?(filepath)
File.delete(filepath)
end
end
end
```
The same applies for `after_create_commit` and `after_update_commit`.
However, if you use the `after_create_commit` and the `after_update_commit`
callback with the same method name, it will only allow the last callback defined
to take effect, as they both internally alias to `after_commit` which overrides
previously defined callbacks with the same method name.
```ruby
class User < ApplicationRecord
after_create_commit :log_user_saved_to_db
after_update_commit :log_user_saved_to_db
private
def log_user_saved_to_db
# This only gets called once
Rails.logger.info("User was saved to database")
end
end
```
```irb
irb> user = User.create # prints nothing
irb> user.save # updating @user
User was saved to database
```
In this case, it's better to use `after_save_commit` instead which is an alias
for using the `after_commit` callback for both create and update:
```ruby
class User < ApplicationRecord
after_save_commit :log_user_saved_to_db
private
def log_user_saved_to_db
Rails.logger.info("User was saved to database")
end
end
```
```irb
irb> user = User.create # creating a User
User was saved to database
irb> user.save # updating user
User was saved to database
```
### Transactional Callback Ordering
By default (from Rails 7.1), transaction callbacks will run in the order they
are defined.
```ruby
class User < ActiveRecord::Base
after_commit { Rails.logger.info("this gets called first") }
after_commit { Rails.logger.info("this gets called second") }
end
```
However, in prior versions of Rails, when defining multiple transactional
`after_` callbacks (`after_commit`, `after_rollback`, etc), the order in which
the callbacks were run was reversed.
If for some reason you'd still like them to run in reverse, you can set the
following configuration to `false`. The callbacks will then run in the reverse
order. See the [Active Record configuration
options](configuring.html#config-active-record-run-after-transaction-callbacks-in-order-defined)
for more details.
```ruby
config.active_record.run_after_transaction_callbacks_in_order_defined = false
```
NOTE: This applies to all `after_*_commit` variations too, such as
`after_destroy_commit`.
[`after_create_commit`]:
https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_create_commit
[`after_destroy_commit`]:
https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_destroy_commit
[`after_save_commit`]:
https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_save_commit
[`after_update_commit`]:
https://api.rubyonrails.org/classes/ActiveRecord/Transactions/ClassMethods.html#method-i-after_update_commit
Callback Objects
----------------
Sometimes the callback methods that you'll write will be useful enough to be
reused by other models. Active Record makes it possible to create classes that
encapsulate the callback methods, so they can be reused.
Here's an example of an `after_commit` callback class to deal with the cleanup
of discarded files on the filesystem. This behavior may not be unique to our
`PictureFile` model and we may want to share it, so it's a good idea to
encapsulate this into a separate class. This will make testing that behavior and
changing it much easier.
```ruby
class FileDestroyerCallback
def after_commit(file)
if File.exist?(file.filepath)
File.delete(file.filepath)
end
end
end
```
When declared inside a class, as above, the callback methods will receive the
model object as a parameter. This will work on any model that uses the class
like so:
```ruby
class PictureFile < ApplicationRecord
after_commit FileDestroyerCallback.new
end
```
Note that we needed to instantiate a new `FileDestroyerCallback` object, since
we declared our callback as an instance method. This is particularly useful if
the callbacks make use of the state of the instantiated object. Often, however,
it will make more sense to declare the callbacks as class methods:
```ruby
class FileDestroyerCallback
def self.after_commit(file)
if File.exist?(file.filepath)
File.delete(file.filepath)
end
end
end
```
When the callback method is declared this way, it won't be necessary to
instantiate a new `FileDestroyerCallback` object in our model.
```ruby
class PictureFile < ApplicationRecord
after_commit FileDestroyerCallback
end
```
You can declare as many callbacks as you want inside your callback objects.
|