Mastering Rails Callbacks – Deciphering the Secrets of Active Record
In this episode of Ruby for All, Andrew and Julie discuss the intricacies of callbacks in Active Record models. They talk about their experiences, the pros and cons of using callbacks, and the issues they faced. They also share some helpful use cases for callbacks, including user authentication, logging and auditing, custom slug generation, and the concept of “hooks.” Also, Andrew and Julie review their ways of dealing with callbacks testing and debugging in a Rails application. Press download now to hear more!
[00:02:32] Let’s learn about “callbacks” in Rails as Andrew explains what they are and uses an example of a blog post to explain how a callback might function when saving a post.
[00:03:56] Julie inquires if Tiptap can be used in a browser for apps and they discuss before-save callbacks in a post model and how they can be used to extract and save a title.
[00:06:19] Andrew elaborates on the three different types of callbacks: before, after, and around callbacks, and gives examples of each.
[00:10:06] They discuss practical uses for before-validation callbacks, such as setting default values.
[00:11:12] Andrew clarifies the concept of “hooks” in programming, comparing it to callbacks.
[00:12:18] Julie asks for examples of actions taken after validation versus before validation.
[00:13:19] Andrew talks about how a file upload, an after-create callback can be used for processing the file such as generating thumbnails or updating related resources. He lists examples use cases for callbacks like hashing passwords before saving to the database during user authentication, triggering email notifications after a comment is posted, and logging and auditing activities like user sign-ups or errors.
[00:15:57] Julie is curious about whether deleted accounts really remove all user data or just make it as inaccessible, noting some services offer a soft delete option with a time window to recover the account. Andrew has not yet encountered the fallback log issue he set up but explains how before-destroy callbacks could be used to implement a time-based soft delete system.
[00:17:19] Andrew describes using before-create callbacks for generating custom slugs for blog posts automatically.
[00:17:54] Andrew recalls a discussion at RailsConf about the diverse opinions on using callbacks, with some developers strongly against them and others in favor. He acknowledges that while callbacks can simplify complex operations, they can also make debugging difficult and can become problematic if used excessively or inappropriately.
[00:23:00] Julie asks Andrew where he stands on the use of callbacks, and he positions himself in the middle, closer to using them when appropriate.
[00:25:16] Andrew emphasizes being cautious with callbacks and explains that callbacks are useful when certain actions need to happen automatically without explicit instruction every time a record is saved.
[00:27:40] Andrew discusses the challenges of testing callbacks, as they can require additional setup in tests and slow down the test suite. He concludes that callbacks are an integral part of Rails, he advises against using them as the first solution and recommends weighing their pros and cons carefully.
Panelists:
Andrew Mason
Julie J.
Sponsors:
Links:
- (00:00) - Intro and Topic Overview
- (02:32) - Introduction to Callbacks in Rails
- (03:56) - Discussing Before-Save Callbacks and Tiptap
- (06:19) - Types of Callbacks: Before, After, and Around
- (10:06) - Uses for Before-Validation Callbacks
- (11:12) - Hooks vs Callbacks
- (12:18) - Practical Use Cases for Callbacks
- (15:57) - Soft Delete Options and Before-Destroy Callbacks
- (17:19) - Generating Custom Slugs with Before-Create Callbacks
- (17:54) - Diverse Opinions on Using Callbacks from RailsConf
- (23:00) - Andrew's Not an Expert
- (25:16) - Caution and Appropriate Use of Callbacks
- (27:40) - Challenges of Testing Callbacks
Creators and Guests
Host
Andrew Mason
Senior Product Developer at Podia, co-host of the Remote Ruby podcast, and co-editor of the Ruby Radar newsletter