Conceptual image depicting the avoidance of Rails Observers in Ruby on Rails development. Features a prominent red ‘No’ symbol over the Rails Observers icon, set against a backdrop highlighting alternative methods like Active Model callbacks. Emphasizes a shift towards more efficient, modern development techniques in Rails.
Choosing Modern Techniques Over Rails Observers: A Ruby on Rails Development Strategy

Avoiding Rails Observers: Better Alternatives

Alessio Bussolari
3 min readNov 21, 2023

--

In the realm of Ruby on Rails development, certain practices and patterns that were once widely used have fallen out of favor due to their limitations and potential for making code less maintainable. One such practice is the use of Rails observers. In this article, we’ll discuss why Rails observers are discouraged and explore alternative, more modern approaches for handling model callbacks.

What Are Rails Observers?

Rails observers are a way to add behavior to model classes based on changes in their state. They allow you to “observe” specific events in a model’s lifecycle, such as before_save or after_create, and execute custom code in response to those events. While they might sound useful in theory, they have several drawbacks that have led the Rails community to discourage their use.

Lack of Clarity

Rails observers can make code less clear and more challenging to understand. When callbacks are defined within observers, it’s not immediately evident where and when those callbacks will be triggered. This lack of clarity can lead to confusion for developers trying to understand the flow of the application.

Hidden Dependencies

--

--

Alessio Bussolari

Ruby on Rails programmer since 2009. Current CTO at COSMIC SRL, where I lead the team in creating innovative solutions.