This skill helps you design robust Rails models with clear associations, validations, scopes, and safe migrations for maintainable apps.
npx playbooks add skill ag0os/rails-dev-plugin --skill rails-model-patterns
Analyze and recommend ActiveRecord model patterns for well-structured Rails applications.
Where a model's domain logic and side effects belong forks on Axis A — logic placement:
rails-stack-profiles (Axis A — native or extracted).native → [decomposition.native.md](decomposition.native.md)extracted → [decomposition.extracted.md](decomposition.extracted.md)Default to native if the axis cannot be resolved. Everything else in this file is invariant.
:dependent: Prevent orphaned records:inverse_of: Ensure bidirectional association consistencyFollow standard Rails model organization. Key ordering: constants, associations, validations, scopes, callbacks, class methods, instance methods, private methods. Generate standard ActiveRecord associations, validations, and scopes following Rails conventions.
Instead of a closed boolean, use a related model. Stores who, when, and why.
class Card < ApplicationRecord
has_one :closure, dependent: :destroy
def closed? = closure.present?
def close!(by:, reason: nil) = create_closure!(creator: by, reason: reason)
def reopen! = closure&.destroy!
end
class Closure < ApplicationRecord
belongs_to :card
belongs_to :creator, class_name: "User"
end
Reduce controller boilerplate using default: with lambdas:
class Post < ApplicationRecord
belongs_to :creator, class_name: "User", default: -> { Current.user }
belongs_to :account, default: -> { Current.account }
end
# Controller: @post = Post.create!(post_params) — no need to assign creator
# migration: add_column :users, :posts_count, :integer, default: 0, null: false
belongs_to :user, counter_cache: true
| Anti-Pattern | Problem | Fix |
|-------------|---------|-----|
| Fat models (500+ lines) | Hard to maintain | Decompose — see the decomposition guide for your axis |
| Callbacks with complex orchestration | Hard to reason about | Simplify the callback — see the decomposition guide for your axis |
| Missing :dependent on associations | Orphaned records | Always specify :dependent |
| default_scope | Confusing query behavior | Use named scopes instead |
| Validations without DB constraints | Data integrity gaps | Add both layers |
| Boolean flags for state | No audit trail, no metadata | Use state-as-relationship pattern (Closure, Pin, Triage models) |
When analyzing or creating models, provide:
project-conventions fingerprint (Minitest/RSpec, fixtures/factories); do not infer it from architecturesave (returns false) for user-facing forms; save! (raises) inside transactionsActiveRecord::Base.transactionActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound, ActiveRecord::StaleObjectErrorlock_version) for concurrent updatesWeekly roundup of top Claude Code skills, MCP servers, and AI coding tips.