active-record-associations

Define and manage Active Record associations in Rails with foreign keys and eager loading.

5|Updated Jan 28, 2026
One-click install
npx skills add https://github.com/ThinkOodle/rails-skills --skill active-record-associations
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: active-record-associations
Source: https://github.com/ThinkOodle/rails-skills/tree/main/skills/active-record-associations
Command: npx skills add https://github.com/ThinkOodle/rails-skills --skill active-record-associations

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes scripts (resource) and references (resource) components.

What problem does it solve?

This Skill provides expert guidance for defining, using, and troubleshooting associations between Active Record models in Ruby on Rails applications, ensuring data integrity and efficient querying.

Core Features & Use Cases

  • Define Relationships: Set up one-to-one, one-to-many, many-to-many, and polymorphic associations.
  • Optimize Queries: Prevent N+1 query problems using eager loading techniques (includes, preload, eager_load).
  • Ensure Data Integrity: Implement dependent: options, database foreign keys, and unique constraints.
  • Use Case: When building an e-commerce platform, you need to define how Products belong to Categoryies and how Orders contain multiple OrderItems, ensuring that deleting a Category doesn't leave orphaned Products.

Quick Start

Use the active-record-associations skill to define a has_many :books association on the Author model and a belongs_to :author association on the Book model, including a dependent: :destroy option and a database foreign key.

Frequently Asked Questions about active-record-associations

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

FAQPage Schema
How do I prevent N+1 query problems in Rails using eager loading?

Prevent N+1 queries in Rails by applying eager loading strategies like `includes`, `preload`, or `eager_load` when fetching associated Active Record models. This ensures associations are loaded in a minimal number of database queries rather than one per record.

When do I need polymorphic associations in Active Record?

You need polymorphic associations in Active Record when a single model belongs to multiple different models. This allows a model to belong_to more than one other type of model on a single association, avoiding the need for multiple specialized foreign keys.

What is the best way to set up a has_many :through association in Rails?

Set up a `has_many :through` association in Rails by creating a join model and defining `belongs_to` on both related models. This establishes a many-to-many connection, allowing you to work directly with the intermediate association.

How do I use dependent options to prevent orphaned records in Rails?

Use `dependent:` options like `:destroy` or `:delete_all` on your Active Record associations to prevent orphaned records. This ensures that when a parent record is deleted, its associated child records are also automatically removed from the database.

Can I use counter caches with Rails associations to track record counts?

Yes, you can use counter caches with Rails associations to efficiently track the number of associated records. By adding a counter cache column, Active Record automatically maintains the count, avoiding expensive database count queries on the fly.