Deep dive into your ActiveRecord models with comprehensive introspection of callbacks, enums, concerns, scopes, validations, and lifecycle hooks.
The introspect command provides a complete view of your model's internal structure, making it easy to understand complex models without digging through code files. Perfect for onboarding, debugging, and understanding model behavior.
# Full introspection of a model
introspect User
# Get specific information
introspect User, :callbacks # Show only callbacks
introspect User, :enums # Show only enums
introspect User, :concerns # Show only concerns/modules
introspect User, :scopes # Show only scopes
introspect User, :validations # Show only validations
# Find where a method is defined
introspect User, :full_nameView all callbacks with their execution order, conditions, and types:
introspect User, :callbacksOutput includes:
before_validation,after_validationbefore_save,around_save,after_savebefore_create,around_create,after_createbefore_update,around_update,after_updatebefore_destroy,around_destroy,after_destroyafter_commit,after_rollbackafter_find,after_initialize,after_touch- Conditional callbacks (
:if,:unless)
See all enum definitions with their mappings and types:
introspect User, :enumsShows:
- Enum name and values
- Type (integer or string)
- Value mappings
- All possible states
Discover all included modules and concerns:
introspect User, :concernsDisplays:
- All concerns included in the model
- Parent classes in inheritance chain
- Modules mixed in
- Source file locations
- Whether it's a concern, class, or module
View all scopes with their SQL:
introspect User, :scopesProvides:
- Scope name
- Generated SQL
- Scope conditions and values
- WHERE, ORDER, LIMIT, INCLUDES clauses
Comprehensive validation information:
introspect User, :validationsIncludes:
- Validation type (presence, uniqueness, length, etc.)
- Attributes being validated
- Options (allow_nil, allow_blank, etc.)
- Conditions (if, unless)
- Validator-specific options
Find where any method is defined:
introspect User, :full_nameReturns:
- File path
- Line number
- Owner class/module
- Type (model, concern, gem, parent)
introspect UserOutput:
═══════════════════════════════════════════════════════════
🔍 MODEL INTROSPECTION: User
═══════════════════════════════════════════════════════════
📊 Lifecycle Summary:
Callbacks: 12
Validations: 8
✓ Has state machine
🔔 Callbacks:
before_validation (2):
1. normalize_email (if: :email_changed?)
2. strip_whitespace
after_create (3):
1. send_welcome_email
2. create_default_profile
3. track_signup
after_commit (1):
1. flush_cache
✅ Validations:
email:
- PresenceValidator
- UniquenessValidator (case_sensitive: false)
- FormatValidator (with: /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i)
password:
- PresenceValidator (on: :create)
- LengthValidator (minimum: 8)
🔢 Enums:
status [Integer]:
active, inactive, suspended, deleted
Mapping: active => 0, inactive => 1, suspended => 2, deleted => 3
role [String]:
user, admin, moderator
🔭 Scopes:
active:
└─ SQL: SELECT "users".* FROM "users" WHERE "users"."status" = 0
└─ where: status = 0
recent:
└─ SQL: SELECT "users".* FROM "users" ORDER BY "users"."created_at" DESC LIMIT 10
└─ order: created_at DESC
└─ limit: 10
📦 Concerns & Modules:
Concerns:
● Authenticatable app/models/concerns/authenticatable.rb:3
● Trackable app/models/concerns/trackable.rb:1
Classes:
▪ ApplicationRecord app/models/application_record.rb:1
═══════════════════════════════════════════════════════════
Generated: 2025-11-13 10:30:45
💡 Tip: Use introspect User, :callbacks to see only callbacks
Use introspect User, :method_name to find where a method is defined
Export introspection data to various formats:
result = introspect User
# Export to JSON
result.to_json
result.to_json(pretty: true)
# Export to YAML
result.to_yaml
# Export to HTML
result.to_html
# Export to file
result.export_to_file('user_introspection.json')
result.export_to_file('user_introspection.html', format: :html)
# Or use the export command
export introspect(User), 'user_introspection.json'Access introspection data programmatically:
result = introspect User
# Check what's available
result.has_callbacks? # => true
result.has_enums? # => true
result.has_concerns? # => true
result.has_scopes? # => true
result.has_validations? # => true
# Get specific data
result.callbacks_by_type(:before_save)
result.validations_for(:email)
result.enum_values(:status)
result.scope_sql(:active)
# Find method source
result.method_source(:full_name)
# => { file: "app/models/user.rb", line: 42, owner: "User", type: :model }# Quickly understand a complex model
introspect Order# See callback execution order
introspect User, :callbacks# View all enum states
introspect Order, :enums# Check what validations are in place
introspect User, :validations# Where is this method coming from?
introspect User, :authenticate
# Shows if it's in the model, a concern, or a gem# See what SQL a scope generates
introspect Product, :scopesThe introspection system handles:
- Abstract classes - Validates model is not abstract
- Missing tables - Checks table existence
- STI models - Works with Single Table Inheritance
- Polymorphic associations - Handles correctly
- Proc callbacks - Shows as
<Proc> - Conditional validations - Displays if/unless conditions
- State machine gems - Detects AASM, StateMachines, Workflow
- Namespaced models - Works with module namespaces
- Complex inheritance - Shows full ancestry chain
Enable/disable the introspect command:
# config/initializers/rails_console_pro.rb
RailsConsolePro.configure do |config|
config.introspect_command_enabled = true # default
end- Start with full introspection - Get the complete picture first
- Use filters for focused analysis - Add
:callbacks,:enums, etc. when you know what you need - Export for documentation - Generate HTML reports for team reference
- Method source is powerful - Use it to track down mysterious behavior
- Combine with other commands - Use
schemafor database structure,introspectfor Ruby logic
- Callbacks that are added dynamically at runtime may not appear
- Private methods in concerns may not have accurate source locations
- Some gems that modify ActiveRecord may interfere with introspection
- Very large models (100+ callbacks) may take a moment to process
schema Model- Database schema and structurestats Model- Model statistics and metricsdiff obj1, obj2- Compare two instancesexplain Query- SQL query analysis
Q: Why don't I see any callbacks? A: Ensure your model has callbacks defined. Abstract classes and tableless models may not have callbacks.
Q: Method source shows nil A: Some methods (built-in Rails methods, dynamically defined) don't have accessible source locations.
Q: Scopes appear empty A: Only scopes that don't require arguments are shown. Parameterized scopes are skipped.
Q: Concerns are missing A: Only concerns/modules with identifiable names are shown. Anonymous modules are excluded.
introspect BlogPost
# Shows validations, scopes, associationsintrospect Order
# Shows state machine enums, callbacks, validationsintrospect User, :authenticate
# Shows it comes from Devise gemintrospect Payment, :callbacks
# See exact order of before_save, after_commit, etc.