Member-only story

5 Common Code Smells in Ruby on Rails Applications

and How to Fix Them

Patrick Karsh
4 min readFeb 24, 2024

--

Ruby on Rails is renowned for its ability to help developers build applications quickly and with a clean, straightforward syntax. However, even in the most elegantly designed applications, “code smells” can emerge. These are indications of deeper problems in the code that, if left unaddressed, can lead to a brittle and hard-to-maintain codebase. Here are five common code smells in Ruby on Rails applications and strategies for resolving them.

This is not good for a model’s diet.

Fat Models

What It Is: In Rails applications, the model is often burdened with too many responsibilities, from business logic to callbacks, validations, and database queries. This not only makes the model files cumbersome but also violates the Single Responsibility Principle, leading to a codebase that’s difficult to navigate and maintain.

How to Fix: Refactor by employing service objects, query objects, and form objects. These patterns help to offload responsibilities from models to dedicated classes, each with a single, clear purpose. Service objects handle business logic, query objects encapsulate database queries, and form objects deal…

--

--

Patrick Karsh
Patrick Karsh

Written by Patrick Karsh

NYC-based Ruby on Rails and Javascript Engineer leveraging AI to explore Engineering. https://linktr.ee/patrickkarsh

No responses yet