rails-model-design

Design ActiveRecord models with validations, associations, scopes, and domain methods.

4|Updated Feb 16, 2017
One-click install
npx skills add https://github.com/nekorush14/dotfiles --skill rails-model-design
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: rails-model-design
Source: https://github.com/nekorush14/dotfiles/tree/main/configs/claude/skills/rails-model-design
Command: npx skills add https://github.com/nekorush14/dotfiles --skill rails-model-design

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides guidelines for designing robust ActiveRecord models with validations, associations, scopes, and domain methods to keep business logic in the model layer.

Core Features & Use Cases

  • Associations & Validations: Clear patterns for belongs_to, has_many, and custom validators.
  • Scopes & Class Methods: Encapsulated queries for common filters.
  • Domain Methods: Rich model APIs for business logic.

Quick Start

Create an Order model with associations to User and OrderItems, add validations, scopes, and a sample domain method like confirm!.

Frequently Asked Questions about rails-model-design

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I structure validations and associations in Rails models?

Validations and associations form the foundation of ActiveRecord models. Use belongs_to, has_many, and custom validators to enforce data integrity at the model layer, then add scopes and domain methods to encapsulate business logic and keep controllers thin.

What's the best way to organize scopes and class methods in ActiveRecord?

Scopes and class methods encapsulate reusable query logic in the model. Define scopes for common filters like status or date ranges, and use class methods for complex queries. This keeps query logic in one place and makes controllers easier to read.

How do I add domain methods to Rails models for business logic?

Domain methods are instance methods that express business behavior—like confirm! or calculate_total—directly on the model. They keep business logic in the model layer rather than scattered across controllers or services, making code more maintainable.

When should I use callbacks in Rails models, and what are the limitations?

Callbacks automate actions when records change, but overuse creates hidden dependencies. Use them sparingly for simple triggers; for complex workflows, prefer explicit domain methods or service objects to keep model behavior transparent.

Can I use a fat model approach with large Rails applications?

A fat model approach works well for medium to large Rails applications when combined with clear separation of concerns. Group related validations, scopes, and domain methods logically; extract unrelated business logic into service objects or concerns when a model grows beyond 200-300 lines.

How do I test validations, scopes, and associations in Rails models?

Test validations with valid and invalid attribute combinations, scopes with filtered datasets, and associations by checking related records are correctly loaded. Use RSpec or Minitest to verify each layer independently, ensuring models behave correctly before controllers use them.