DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

The Hidden DSL Inside Every Rails Model

The Hidden DSL Inside Every Rails Model
The Hidden DSL Inside Every Rails Model

June 10, 2026

Most Rails developers use belongs_to, has_many, scope, and validates every day.

We type them almost without thinking.


class User < ApplicationRecord
  belongs_to :company

  validates :email, presence: true

  scope :active, -> { where(active: true) }
end
Enter fullscreen mode Exit fullscreen mode

But here’s something interesting:

None of those are Ruby keywords.

They’re methods.

In fact, a Rails model is less like a traditional Ruby class and more like a Domain Specific Language (DSL) built on top of Ruby.

Once you see it, it’s hard to unsee.

Tokyo Topographic Map<!-- CONTENT + GRADIENT OVERLAY -->

Built for Ruby on Rails

Build Maps Without

Google APIs

Generate beautiful production-ready maps directly from your Rails backend. Fast rendering, zero external dependencies, full control.

View Live Demo →Read Docs

✓ No API fees✓ Self-hosted✓ Rails Native✓ Fast Rendering

Why developers switch

Replace expensive map stacks.

Stop relying on third-party map billing and bloated JS libraries. Render static or dynamic maps directly in Ruby.

Try It Now

Tokyo MapView Demo

The Empty Model

A Rails model starts life as a completely ordinary Ruby class.


class User < ApplicationRecord
end
Enter fullscreen mode Exit fullscreen mode

At first glance, there’s nothing special here.

User inherits from ApplicationRecord, which inherits from ActiveRecord::Base.

That’s where the magic begins.

By inheriting from Active Record, the class gains hundreds of methods that allow us to describe data, relationships, validations, callbacks, and behavior using a concise DSL.

Associations

Consider this:


class User < ApplicationRecord
  belongs_to :company

  has_many :posts
end
Enter fullscreen mode Exit fullscreen mode

When Rails reads these lines, it isn’t creating relationships in the database.

It’s executing methods.

Those methods generate additional behavior behind the scenes.

After declaring belongs_to :company, Rails creates methods such as:


👉 Read the full article.

The Hidden DSL Inside Every Rails Model – Linking Ruby knowledge from the most remote places in the world.

The Hidden DSL Inside Every Rails Model June 10, 2026 Most Rails developers use belongs_to, has_many, scope, and validates every day. We type them almost without thinking. class User < Applicati…

favicon rubystacknews.com

Article content

Top comments (0)