Write Active Record models with associations and validations
✓Works with OpenClaudeYou are a Rails developer writing production-ready Active Record models. The user wants to create well-structured models with proper associations, validations, and database constraints.
What to check first
- Run
rails -vto confirm Rails version (syntax varies slightly between Rails 6 and 7+) - Check your
config/database.ymlto understand your database setup - Run
rails db:createif the database doesn't exist yet
Steps
- Generate a model using
rails generate model Post title:string content:text user:references— this creates the model file and migration with foreign key - Define
belongs_to :userin the Post model to establish the relationship - Add
has_many :posts, dependent: :destroyin the User model to define the reverse association - Add
validates :title, presence: true, length: { minimum: 3, maximum: 100 }to enforce validation rules - Use
validates :email, uniqueness: { case_sensitive: false }, format: { with: URI::MailTo::EMAIL_REGEXP }for email-specific validations - Add
has_many :comments, through: :postsin User model for indirect associations - Create database constraints with
add_index :posts, :user_idandadd_foreign_key :posts, :usersin the migration - Run
rails db:migrateto apply all migrations and constraints
Code
# app/models/user.rb
class User < ApplicationRecord
has_many :posts, dependent: :destroy
has_many :comments, through: :posts
has_secure_password
validates :email, presence: true,
uniqueness: { case_sensitive: false },
format: { with: URI::MailTo::EMAIL_REGEXP }
validates :username, presence: true,
uniqueness: true,
length: { minimum: 3, maximum: 20 }
validates :password, length: { minimum: 6 }, if: -> { new_record? || !password.nil? }
before_save :downcase_email
private
def downcase_email
self.email = email.downcase
end
end
# app/models/post.rb
class Post < ApplicationRecord
belongs_to :user
has_many :comments, dependent: :destroy
has_many :commenters, through: :comments, source: :user
validates :title, presence: true,
length: { minimum: 5, maximum: 150 }
validates :content, presence: true,
length: { minimum: 10 }
validates :user_id, presence: true
scope :recent, -> { order(created_at: :desc) }
scope :published, -> { where(published: true) }
before_validation :strip_whitespace
private
def strip_whitespace
self.title = title&.strip
self
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Ruby on Rails Skills
Other Claude Code skills in the same category — free to download.
Rails Setup
Scaffold Ruby on Rails app with models and controllers
Rails API
Build Rails API-only application with serializers
Rails Testing
Write RSpec tests with factories and mocks
Rails Stimulus
Build interactive UIs with Hotwire and Stimulus
Rails Sidekiq
Set up background jobs with Sidekiq
Rails ActiveRecord Performance Optimization
Fix N+1 queries, slow scopes, and ActiveRecord pitfalls in production Rails apps
Rails Action Cable WebSockets
Build real-time features in Rails using Action Cable WebSockets
Want a Ruby on Rails skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.