
Mastering Rails Callbacks – Deciphering the Secrets of Active Record
02/08/24 • 30 min
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:
Julie J. Website
Active Record Callbacks
- (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
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:
Julie J. Website
Active Record Callbacks
- (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
Previous Episode

New Onboarding Experiences — Julie's Transition to a New Team
In today’s episode, Andrew and Julie dive into the topic of onboarding onto new teams. Julie discusses her latest venture of switching teams, and Andrew sheds light on the innovative “Shape Up” method by Basecamp that’s shaking things up in the project management world, and why he prefers this over Agile. There’s talk of the dreaded technical debt, and how to keep it in check, plus the perks of pair programming and the need for a solid support system at work. Besides tackling these workplace issues, they also touch on the challenge of maintaining personal relationships in a remote working environment, keeping old team ties strong, and why asking questions is key to professional growth. Press download now to hear more!
[00:01:45] Julie gives us an update on the changes at her work, transitioning from a consumer team to a platform team. She mentions that her old team is shifting from two-week sprints to a new process called “Shape Up,” which Andrew explains it as a product/project management philosophy from Basecamp, focusing on a six-week cycle.
[00:03:08] Andrew details the process of shaping a feature, setting boundaries, identifying risks, and then pitching it.
[00:04:25] Julie inquires about the involvement of engineers in the shaping and betting processes, and Andrew describes how it works at Podia, and how they used Flipper.
[00:06:33] Andrew discusses the “cool down” period after a project cycle, which at Podia involves monitoring for bugs and wrapping up the project details rather than no scheduled work.
[00:07:42] The topic of technical debt is addressed, with Andrew acknowledging its inevitability and the importance of staying on top of it through practices like support weeks.
[00:10:54] Andrew expresses preference for the Shape Up process over Agile, appreciating the longer time frames, collaborative problem-solving with designers, and a less stressful experience with more planned projects.
[00:12:14] Julie shares her transition to a new team and the challenges of ramping up, contrasting it with her experience from two years ago and feeling the pressure to not ask basic questions due to her years of experience.
[00:13:53] Julie discusses the pressure she feels to ramp up quickly on her new team, acknowledging its self-imposed. Andrew and Julie talks about the onboarding process, where Julie notes the benefit of scheduled pair programming sessions with teammates as a key part of her learning.
[00:15:44] Andrew shares Podia’s onboarding method, which involves acting like a user of the application to understand its various parts. Julie reflects on the complexity of her new team’s codebase and the challenge of understanding how services interact.
[00:17:51] Andrew suggest creating a service diagram to visualize service interactions, something he found useful in previous jobs. Julie considers the idea and mentions the potential benefits of a detailed visual representation of the service interactions for her understanding.
[00:19:48] Julie and Andrew discuss the social dynamics of joining a new team with established relationships and the extra challenge of doing it so remotely. Andrew shares similar experiences and the importance of being inclusive to new team members.
[00:21:59] Andrew shares how he’s an introvert by nature, and Julie and Andrew both agree on the importance of asking questions and having supportive seniors and leaders who encourage a culture of inquiry.
[00:26:05] Julie talks about maintaining relationships with her old team and the value of keeping professional connections active, even after moving to a new team or company.
Panelists:
Andrew Mason
Julie J.
Sponsors:
Links:
- (00:00) - Intro and Topic Overview
- (01:45) - Julie's Team Transition and Shape Up Method
- (03:08) - Andrew on Shaping a Feature in Shape Up
- (04:25) - Engineer Involvement in Shaping and Betting
- (06:33) - Podia's "Cool Down" Period After Project Cycles
- (07:42) - Addressing Technical Debt
- (10:54) - Preference for Shape Up Over Agile
- (12:14) - Julie's Transition and Ramping Up Challenges
- (13:53) - Onboarding Process and Pair Programming
- (15:44) - Podia's Onboarding Method
- (17:51) - Creating a Service Diagram for Understanding Interactions ...
Next Episode

Ski Slopes, Sorbet, and Copilot — Effective Learning with Ryan Caldwell
In this episode of Ruby for All, Andrew and Julie chat about their recent experiences, including a ski trip with challenges due to a storm, and discuss burnout and returning to regular podcasting. Special guest, Ryan Caldwell, a software engineer at GitHub working on Copilot, joins the conversation to discuss his work, particularly on chat-related features of Copilot. Ryan shares insights on programming languages, leaning into his transitions between Ruby, Java, and Go, and navigating the differences between dynamically and statically typed languages. The conversation covers the benefits and challenges of implementing type checking in Ruby with Sorbet, especially in large projects like GitHub. Ryan advocates for learning Ruby on Rails, praises its efficiency for staring profitable projects, and provides tips for using Copilot Chat effectively. Press download now to hear more!
[00:00:23] Julie fills us in on a recent skiing trip to went on in California, the huge storm they encountered and leaving early to avoid being stranded, the broken chain on their car, and a scary moment on a slope with her kids. Andrew shares he experienced burnout but sees improvement.
[00:02:47] Ryan Caldwell introduces himself and tells us what he does.
[00:03:53] Andrew asks Ryan about the programming languages used for Copilot, leading to a discussion about using Go for its REST API, the manageability of the project, and Ryan’s transition from Codespaces to Copilot after paternity leave.
[00:04:49] Andrew wonders why Go was chosen, and Ryan explains the team’s familiarity with Go and the language’s simplicity.
[00:06:12] Ryan reflects his first programming language and journey through JavaScript, Python, Java, and Ruby, highlighting his appreciation for Ruby. He talks about learning Ruby on the job, and his fondness for Rails.
[00:08:02] Ryan discusses the challenges of picking up new languages and his approach to learning through project involvement.
[00:09:24] Andrew asks about the shift from dynamic to typed languages, and Ryan shares his experiences transitioning from Ruby to Go.
[00:11:53] We hear about Ryan’s work on type checking with Sorbet at GitHub, and he shares that Sorbet helped find edge cases and bugs, improving the code by requiring changes to the structure to prevent these issues.
[00:15:09] Ryan feels the biggest benefit of Sorbet is enforcing developers to consider boundaries and contracts between classes, which encourages thoughtful coding and design. A downside he mentions is the time and confusion involved in the migration process, particularly for team members unfamiliar with the new syntax.
[00:17:11] Julie inquires if Ryan would do anything differently regarding Sorbet implementation. He reflects on the challenge of estimating the time required for implementing Sorbet, dealing with complex code, and the difficulty of refactoring legacy code without comprehensive tests.
[00:18:44] Would Ryan go back to Ruby/Rails without Sorbet? He states that he would for personal projects for speed but appreciates Sorbet in team settings for defining clear code boundaries.
[00:19:31] Ryan suggests that small teams should consider Sorbet if it solves a specific problem, rather than adopting it without a clear purpose.
[00:21:40] Ryan discusses his pride in streamlining the authentication process across different clients in Copilot, leading to a simplified codebase for the team. A tip he shares is to provide as much context as possible when using Copilot Chat to get better responses.
[00:25:35] Andrew talks about custom instructions for ChatGPT, like ensuring all output is in bullet points, and wonders if such a feature exists for Copilot.
[00:28:46] Ryan advises newer developers to be intentional about what they chose to learn in software development, emphasizing the importance of investing learning time wisely. And yes, Ruby on Rails is still worth learning in 2024.
[00:31:03] Find out where you can follow Ryan on the interwebs.
Panelists:
Andrew Mason
Julie J.
Guest:
Ryan Caldwell
Sponsors:
Links:
If you like this episode you’ll love
Episode Comments
Generate a badge
Get a badge for your website that links back to this episode
<a href="https://goodpods.com/podcasts/ruby-for-all-418618/mastering-rails-callbacks-deciphering-the-secrets-of-active-record-58054552"> <img src="https://storage.googleapis.com/goodpods-images-bucket/badges/generic-badge-1.svg" alt="listen to mastering rails callbacks – deciphering the secrets of active record on goodpods" style="width: 225px" /> </a>
Copy