Concerns

Concerns in Ruby on Rails are modules that allow you to extract reusable code from models or controllers. They help keep your application organized by separating shared logic into clean, maintainable components.

Table of Contents

What are Concerns in Ruby on Rails?

Concerns are a way to modularize and reuse code within a Rails application. They allow you to group related methods into modules and include them in multiple models or controllers as needed.

Instead of keeping large chunks of logic inside a single model or controller, concerns help you split them into meaningful, focused pieces thereby making your codebase more readable and maintainable.

Rails encourages this by providing dedicated folders:

  • app/models/concerns/

  • app/controllers/concerns/

These folders automatically load concern modules, making them easy to integrate.

Why are Concerns Useful?

As applications grow, models and controllers can become bloated with responsibilities. Concerns solve this by:

  • Reducing duplication across controllers or models

  • Keeping classes small and focused

  • Improving organization through shared behavior

  • Making large codebases easier to navigate

  • Supporting better testing by isolating responsibilities

Concerns help enforce clean architecture principles without overcomplicating your Rails app.

How Concerns Work

A typical concern is just a Ruby module that includes shared methods or logic.

Example: A model concern for soft deletion

Glossary_Concerns_1.png

Include it in a model:

Glossary_Concerns_2.png

Rails automatically loads concerns, so no manual require is needed.

Where to Use Concerns

  • Sharing authentication logic between multiple controllers

  • Reusing query scopes or methods between models

  • Extracting callbacks or validations that repeat across models

  • Managing shared formatting, caching, or utility behaviors

  • Structuring large applications with common patterns

In Summary

Concerns in Ruby on Rails help keep applications clean and modular by extracting shared logic into reusable modules. They reduce duplication, improve readability, and support maintainable code structures, especially as applications scale.