$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
Ruby on RailsintermediateNew

Rails Active Record

Share

Write Active Record models with associations and validations

Works with OpenClaude

You 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 -v to confirm Rails version (syntax varies slightly between Rails 6 and 7+)
  • Check your config/database.yml to understand your database setup
  • Run rails db:create if the database doesn't exist yet

Steps

  1. Generate a model using rails generate model Post title:string content:text user:references — this creates the model file and migration with foreign key
  2. Define belongs_to :user in the Post model to establish the relationship
  3. Add has_many :posts, dependent: :destroy in the User model to define the reverse association
  4. Add validates :title, presence: true, length: { minimum: 3, maximum: 100 } to enforce validation rules
  5. Use validates :email, uniqueness: { case_sensitive: false }, format: { with: URI::MailTo::EMAIL_REGEXP } for email-specific validations
  6. Add has_many :comments, through: :posts in User model for indirect associations
  7. Create database constraints with add_index :posts, :user_id and add_foreign_key :posts, :users in the migration
  8. Run rails db:migrate to 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

Quick Info

Difficultyintermediate
Version1.0.0
AuthorClaude Skills Hub
railsactive-recordorm

Install command:

curl -o ~/.claude/skills/rails-active-record.md https://clskills.in/skills/rails/rails-active-record.md

Related Ruby on Rails Skills

Other Claude Code skills in the same category — free to download.

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.