activerecord
This skill should be used when the user asks about "ActiveRecord", "database queries", "associations", "validations", "migrations", "scopes", "callbacks", "N+1 queries", "eager loading", "includes", "joins", "eager_load", "preload", "database optimization", "model relationships", "has_many", "belongs_to", "has_one", "polymorphic associations", "pluck", "exists", or needs guidance on database-related Rails topics.
What this skill does
# ActiveRecord
Comprehensive guide to ActiveRecord associations, queries, validations, and database optimization in Rails.
## Associations
### Association Types
| Type | Description |
|------|-------------|
| `belongs_to` | Foreign key on this model's table |
| `has_one` | Foreign key on other model's table (singular) |
| `has_many` | Foreign key on other model's table (plural) |
| `has_many :through` | Many-to-many via join model |
| `has_one :through` | One-to-one via join model |
| `has_and_belongs_to_many` | Many-to-many via join table (no model) |
### Basic Associations
```ruby
class User < ApplicationRecord
has_one :profile, dependent: :destroy
has_many :articles, dependent: :destroy
has_many :comments, dependent: :destroy
end
class Article < ApplicationRecord
belongs_to :user
belongs_to :category, optional: true # Allow nil
has_many :comments, dependent: :destroy
has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
end
```
### Association Options
| Option | Purpose |
|--------|---------|
| `dependent: :destroy` | Delete associated records via callbacks |
| `dependent: :delete_all` | Delete directly via SQL (no callbacks) |
| `dependent: :nullify` | Set foreign key to NULL |
| `dependent: :restrict_with_error` | Add error if associated records exist |
| `dependent: :restrict_with_exception` | Raise exception if associated |
| `optional: true` | Allow nil belongs_to (required by default in Rails 5+) |
| `inverse_of` | Specify inverse association for bidirectional optimization |
| `counter_cache: true` | Maintain count column automatically |
| `touch: true` | Update parent's `updated_at` on changes |
| `class_name` | Specify associated class when name differs |
| `foreign_key` | Specify custom foreign key column |
### Has Many Through
```ruby
class Doctor < ApplicationRecord
has_many :appointments
has_many :patients, through: :appointments
end
class Patient < ApplicationRecord
has_many :appointments
has_many :doctors, through: :appointments
end
class Appointment < ApplicationRecord
belongs_to :doctor
belongs_to :patient
# Join model can have its own attributes: appointment_date, notes, etc.
end
```
### Polymorphic Associations
```ruby
class Comment < ApplicationRecord
belongs_to :commentable, polymorphic: true
end
class Article < ApplicationRecord
has_many :comments, as: :commentable
end
class Photo < ApplicationRecord
has_many :comments, as: :commentable
end
# Migration
create_table :comments do |t|
t.references :commentable, polymorphic: true, index: true
t.text :body
t.timestamps
end
```
### Self-Referential
```ruby
class Employee < ApplicationRecord
belongs_to :manager, class_name: "Employee", optional: true
has_many :subordinates, class_name: "Employee", foreign_key: "manager_id"
end
```
## Validations
### Built-in Validation Helpers
```ruby
class User < ApplicationRecord
# Presence - not empty (uses Object#blank?)
validates :name, presence: true
# Absence - must be blank
validates :spam_flag, absence: true
# Acceptance - checkbox must be checked
validates :terms_of_service, acceptance: true
# Confirmation - two fields must match
validates :email, confirmation: true
# Requires email_confirmation field in form
# Uniqueness (case-insensitive)
validates :email, uniqueness: { case_sensitive: false, scope: :account_id }
# Format - regex match
validates :email, format: { with: URI::MailTo::EMAIL_REGEXP }
# Length
validates :password, length: { minimum: 8, maximum: 72 }
validates :bio, length: { maximum: 500, too_long: "%{count} characters max" }
validates :code, length: { is: 6 }
validates :name, length: { in: 2..50 }
# Numericality
validates :age, numericality: { only_integer: true, greater_than: 0 }
validates :price, numericality: { greater_than_or_equal_to: 0 }
# Inclusion - value must be in list
validates :role, inclusion: { in: %w[admin editor viewer] }
# Exclusion - value must NOT be in list
validates :subdomain, exclusion: { in: %w[www admin api] }
# Comparison - compare to another attribute or value
validates :end_date, comparison: { greater_than: :start_date }
validates :age, comparison: { greater_than_or_equal_to: 18 }
# Associated records must also be valid
validates_associated :profile
end
```
### Common Validation Options
| Option | Purpose |
|--------|---------|
| `:message` | Custom error message (supports `%{value}`, `%{attribute}`, `%{model}`) |
| `:on` | When to validate: `:create`, `:update`, or custom context |
| `:allow_nil` | Skip validation if value is `nil` |
| `:allow_blank` | Skip validation if value is blank |
| `:if` / `:unless` | Conditional validation (symbol, proc, or array) |
| `:strict` | Raise `ActiveModel::StrictValidationFailed` instead of adding error |
### Conditional Validations
```ruby
class Order < ApplicationRecord
validates :shipping_address, presence: true, if: :requires_shipping?
validates :credit_card, presence: true, unless: :free_order?
# Proc
validates :coupon_code, presence: true, if: -> { discount_percentage.present? }
# Multiple conditions (all :if must pass AND none of :unless)
validates :phone, presence: true, if: [:contact_by_phone?, :phone_required?]
# Group validations
with_options if: :premium_user? do
validates :credit_card, presence: true
validates :billing_address, presence: true
end
end
```
### Custom Validations
```ruby
class User < ApplicationRecord
validate :email_domain_allowed
validates_with EmailValidator
private
def email_domain_allowed
return if email.blank?
domain = email.split("@").last
errors.add(:email, "must be from an allowed domain") unless allowed_domain?(domain)
end
end
# Custom validator class
class EmailValidator < ActiveModel::Validator
def validate(record)
unless record.email.include?("@")
record.errors.add(:email, "must contain @")
end
end
end
```
## Scopes
```ruby
class Article < ApplicationRecord
scope :published, -> { where(status: "published") }
scope :draft, -> { where(status: "draft") }
scope :recent, -> { order(created_at: :desc) }
# With arguments
scope :by_author, ->(author) { where(author: author) }
scope :created_after, ->(date) { where(created_at: date..) }
# With defaults
scope :limit_recent, ->(count = 10) { recent.limit(count) }
# Combining
scope :featured, -> { published.where(featured: true).recent }
end
# Chainable
Article.published.by_author(user).recent.limit(5)
```
## Queries
### Finding Records
```ruby
# Single record
User.find(1) # Raises RecordNotFound if missing
User.find_by(email: "[email protected]") # Returns nil if missing
User.find_by!(email: "[email protected]") # Raises if missing
User.first # First by primary key
User.last # Last by primary key
User.take # Any record (no ordering)
# Collections
User.where(status: "active")
User.where.not(role: "admin")
User.where(created_at: 1.week.ago..) # Range (>= 1 week ago)
User.where(age: 18..65) # BETWEEN
# OR conditions
User.where(role: "admin").or(User.where(role: "editor"))
# Selection
User.select(:id, :email, :name) # Specific columns
User.distinct # Remove duplicates
```
### Efficient Data Extraction
```ruby
# pluck - returns array of values (no model instantiation)
User.pluck(:email) # ["[email protected]", "[email protected]"]
User.pluck(:id, :email) # [[1, "[email protected]"], [2, "[email protected]"]]
User.where(active: true).pluck(:id)
# ids - shortcut for pluck(:id)
User.where(active: true).ids # [1, 2, 3]
# exists? - boolean check without loading records
User.exists?(email: "[email protected]") # true/false
User.where(role: "admin").exists? # true/false
# count/sum/average/minimum/maximum
Order.count
Order.sum(:total)
Order.average(:total)
Order.maximum(:created_at)
```
#Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.