This skill should be used when the user asks to "write a migration", "add a column", "add column to table", "create an index", "add a foreign key", "set up associations", "fix N+1 queries", "optimize queries", "add validations", "create callbacks", "use eager loading", or mentions ActiveRecord, belongs_to, has_many, has_one, :through associations, polymorphic associations, inverse_of, touch: true, counter_cache, dependent: destroy, where clauses, scopes, includes, preload, eager_load, joins, find_each, batch processing, counter caches, foreign key constraints, or database constraints. Should also be used when editing *_model.rb files, working in app/models/ directory, db/migrate/, discussing query performance, N+1 prevention, validation vs constraint decisions, or reviewing database schema design.
View on GitHubhoblin/claude-ruby-marketplace
activerecord
February 1, 2026
Select agents to install to:
npx add-skill https://github.com/hoblin/claude-ruby-marketplace/blob/main/plugins/activerecord/skills/activerecord/SKILL.md -a claude-code --skill activerecordInstallation paths:
.claude/skills/activerecord/# ActiveRecord
This skill provides comprehensive guidance for working with ActiveRecord in Rails applications. Use for writing migrations, defining associations, optimizing queries, preventing N+1 issues, implementing validations, and following database best practices.
## Quick Reference
### CRUD Operations
```ruby
# Create
user = User.create(name: "Alice", email: "alice@example.com")
user = User.create!(...) # Raises on failure
# Read
User.find(1) # Raises RecordNotFound
User.find_by(email: "x") # Returns nil if not found
User.where(active: true) # Returns Relation
# Update
user.update(name: "Bob")
user.update!(...) # Raises on failure
# Delete (callbacks run)
user.destroy
# Delete (no callbacks)
user.delete
```
### Key Concepts
| Concept | Purpose |
|---------|---------|
| `belongs_to` | Child side of association (has foreign key) |
| `has_many` / `has_one` | Parent side of association |
| `has_many :through` | Many-to-many via join model |
| `includes` / `preload` | Eager loading (prevent N+1) |
| `scope` | Named query builder |
| `validates` | Model-level data validation |
| `before_save` / `after_commit` | Lifecycle callbacks |
## Eager Loading Decision Tree
```
Need to access associated data?
├── NO → Use `joins` (filtering only)
└── YES → Need to filter/sort by association?
├── NO → Use `preload` (separate queries)
└── YES → Large dataset with many associations?
├── YES → Use `includes` with `references`
└── NO → Use `eager_load` (single JOIN)
```
### Quick Comparison
| Method | Strategy | Best For |
|--------|----------|----------|
| `includes` | Auto-choose | Default choice |
| `preload` | Separate queries | Large datasets, no filtering |
| `eager_load` | LEFT OUTER JOIN | Filtering by association |
| `joins` | INNER JOIN | Filtering only, not accessing data |
```ruby
# N+1 problem
Post.all.each { |p| p.author.name } # 1 + N queries
# Solution
Post.includes(