models

Enforce Rails model patterns for associations, validations, scopes, and concerns.

Updated Dec 13, 2024
One-click install
npx skills add https://github.com/apdarr/lace --skill models-apdarr
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: models
Source: https://github.com/apdarr/lace/tree/main/.github/skills/models
Command: npx skills add https://github.com/apdarr/lace --skill models-apdarr

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Rails model design can be complex and error-prone; this guide consolidates ActiveRecord patterns, validations, callbacks, scopes, associations, concerns, query objects, and form objects into a coherent, maintainable approach.

Core Features & Use Cases

  • Master patterns for ActiveRecord associations, validations, scopes, and callbacks to ensure data integrity and scalable business logic.
  • Use concerns, query objects, and form objects to split responsibilities, improve testability, and promote reusable components across models.
  • Real-world use cases include building rich domain models with clean boundaries, extracting complex queries, and avoiding common AR pitfalls like N+1 problems and overbearing callbacks.

Quick Start

Define a model applying the patterns for associations, validations, scopes, concerns, and validators and place related query objects and form objects in their respective folders.

Frequently Asked Questions about models

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

FAQPage Schema
How do I structure Rails models to avoid complex and error-prone business logic?

Structure Rails models by extracting complex logic into concerns, query objects, and form objects, ensuring clear separation of concerns. This approach maintains data integrity and promotes reusable, testable components across your ActiveRecord domain.

When do I need query objects and form objects in ActiveRecord?

You need query objects and form objects in ActiveRecord when extracting complex queries or handling multi-model forms to split responsibilities. Placing them in dedicated folders improves testability and promotes reusable components across your Rails application.

What's the best way to handle ActiveRecord validations and database constraints?

Handle ActiveRecord validations and database constraints by enforcing a comprehensive set of patterns across your models. This ensures data integrity and scalable business logic while maintaining clean boundaries between ActiveRecord and ActiveModel semantics.

Does this Rails model pattern approach work for domain-rich applications?

Yes, this Rails model pattern approach works specifically for typical domain-rich Rails applications where reusable components, testability, and maintainable business logic are critical. It enables consistent model design across services with clear separation of concerns.

How do I prevent common ActiveRecord pitfalls like N+1 problems and overbearing callbacks?

Prevent ActiveRecord pitfalls like N+1 problems and overbearing callbacks by applying lightweight callbacks and extracting complex queries into query objects. This maintains clean boundaries and avoids common AR pitfalls in your domain models.

Can I use custom validators and concerns to split responsibilities in Rails models?

Yes, you can use custom validators and concerns to split responsibilities in Rails models. This promotes reusable components and improves testability by enforcing clear separation of concerns with dedicated folders for validators and concerns.