惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
G
Google Developers Blog
H
Help Net Security
月光博客
月光博客
阮一峰的网络日志
阮一峰的网络日志
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
GbyAI
GbyAI
D
Docker
Hugging Face - Blog
Hugging Face - Blog
I
InfoQ
博客园 - 司徒正美
Last Week in AI
Last Week in AI
Microsoft Security Blog
Microsoft Security Blog
美团技术团队
Stack Overflow Blog
Stack Overflow Blog
M
MIT News - Artificial intelligence

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Never Use Service Classes in Rails
Hulk in Publ · 2026-05-21 · via DEV Community

Service classes are one of the most representative anti-patterns in Rails.
That is because they deviate from Rails' MVC principles.

You have probably heard of the Single Responsibility Principle.
In MVC, each component has its own role:

  • Model: storing, managing, processing, and relating data
  • View: display, templates, and user interface
  • Controller: handling HTTP requests

So, if we introduce a service class, what is its responsibility?
I do not know.

The name "service" creates the illusion that you can write some impressive-sounding business logic there.
However, the name "service" has no meaning or definition.
Why are so many people strict about meaningless variable names like tmp or x, yet willing to accept a meaningless class called a "service class"?

In my more than five years of experience with Ruby on Rails, service classes had unfortunately already been created in several projects.
In many of those cases, complex data-processing logic had been written in controllers, and service classes were used simply to clean up fat controllers.

That premise is wrong.
If you write business logic in models—especially logic related to retrieving and processing data—and keep controllers focused solely on calling that logic, controllers will never become complex.

"Do you accept fat models?"
Yes, of course.
In real companies, "business logic" is, in other words, a discussion about "how to handle data," which is precisely the role of the model.
When business requirements are large, it is inevitable that models become complex and grow in size.

Implementing readable code within models is where we engineers show our skill.
There is never any place for service classes.

"What should you do if a model grows so large that a single file exceeds 1,000 lines?"
Please refer to the OSS code published by DHH and 37signals.
Even when we talk about fat models, that does not mean you should cram all logic into a single class.

Campfire

Campfire is a web-based chat application. It supports many of the features you'd expect, including:

  • Multiple rooms, with access controls
  • Direct messages
  • File attachments with previews
  • Search
  • Notifications (via Web Push)
  • @mentions
  • API, with support for bot integrations

Deploying with Docker

Campfire's Docker image contains everything needed for a fully-functional single-machine deployment. This includes the web app, background jobs, caching, file serving, and SSL.

To persist storage of the database and file attachments, map a volume to /rails/storage.

To configure additional features, you can set the following environment variables:

  • SSL_DOMAIN - enable automatic SSL via Let's Encrypt for the given domain name
  • DISABLE_SSL - alternatively, set DISABLE_SSL to serve over plain HTTP
  • VAPID_PUBLIC_KEY/VAPID_PRIVATE_KEY - set these to a valid keypair to allow sending Web Push notifications. You can generate a new keypair by running /script/admin/create-vapid-key
  • SENTRY_DSN - to enable error reporting to sentry in production, supply…

For example, once-campfire defines the following files related to the User model:

  • models/user.rb
  • models/user/avatar.rb
  • models/user/bannable.rb
  • models/user/bot.rb
  • models/user/mentionalbe.rb
  • models/user/role.rb
  • models/user/transferable.rb

The first file, models/user.rb, is the one you are familiar with—the file created by rails g model User.
The others are modules that include ActionText::Attachable. In other words, they are concerns.

Even if something is not shared with other models, it should be extracted into a concern and split out.
That way, the main models/user.rb can remain simple.


By the way, who was it that started advocating for service classes in the first place?
DHH was loudly criticizing service classes, wasn't he?

When I ask AI for advice on Rails refactoring, it forcibly suggests services even though I never asked for them.

It is extremely irritating.
It makes me realize that the expectation that AI will replace real engineers is nothing more than nonsense.