Every ActiveRecord model inherits a rich set of class-level methods from Rails. When you type Content. in a Rails console and hit Tab, you’ll see hundreds of possibilities. This article organizes them by category so you know what’s available and when to use each group.
Where These Methods Come From
A minimal ActiveRecord model:
class Content < ActiveRecord::Base
end
…inherits methods from roughly a dozen Rails modules:
ActiveRecord::Querying—find,where,select,order,joins, etc.ActiveRecord::Callbacks—before_save,after_create,around_destroy, etc.ActiveRecord::Validations—validates,validate,validates_presence_of, etc.ActiveRecord::Associations—has_many,belongs_to,has_one,has_and_belongs_to_manyActiveRecord::Scoping—scope,default_scope,unscopedActiveRecord::AttributeMethods—attribute,attr_readonly,column_for_attributeActiveRecord::ConnectionHandling—connection,establish_connection,connection_poolActiveRecord::Core—find,create,new,destroy,inspectActiveSupport::Callbacks—define_callbacks,set_callback,skip_callback- Ruby’s
ModuleandObject—ancestors,include,extend,method_defined?, etc.
Querying Methods
These are what you use to build SQL queries:
Content.all # SELECT * FROM contents
Content.where(status: "published") # filtered query
Content.where("created_at > ?", 1.week.ago)
Content.order(created_at: :desc)
Content.limit(10).offset(20) # pagination
Content.select(:id, :title, :created_at) # specific columns
Content.joins(:author).where(authors: { active: true })
Content.includes(:tags) # eager load associations
Content.distinct
Content.pluck(:title) # returns array of values
Content.count # SELECT COUNT(*)
Content.sum(:word_count)
Content.average(:rating)
Content.minimum(:price)
Content.maximum(:views)
Content.exists?(id: 42)
Content.first; Content.last; Content.fifth
Content.find(42) # raises RecordNotFound if missing
Content.find_by(slug: "hello-world") # returns nil if missing
Content.find_or_create_by(slug: "test")
Content.scope :published, -> { where(status: "published") }
Callback Registration
Rails callbacks let you hook into the object lifecycle:
Content.before_save :normalize_title
Content.after_create :send_notification
Content.before_destroy :check_dependencies
Content.around_update :log_changes
# All available callback hooks:
# before/after/around: create, update, save, destroy, validate, find, initialize
# Also: after_commit, after_rollback, before_commit
Validation DSL
Content.validates :title, presence: true, length: { maximum: 200 }
Content.validates :slug, uniqueness: true, format: { with: /\A[a-z0-9-]+\z/ }
Content.validates_presence_of :author_id
Content.validates_uniqueness_of :slug, case_sensitive: false
Content.validate :custom_validation_method
Association Macros
Content.belongs_to :author
Content.has_many :comments, dependent: :destroy
Content.has_many :tags, through: :content_tags
Content.has_one :metadata
Content.has_and_belongs_to_many :categories
Content.accepts_nested_attributes_for :comments
Schema and Column Introspection
Useful for understanding the model’s database schema at runtime:
Content.column_names # => ["id", "title", "body", "created_at", ...]
Content.columns # => [#<ActiveRecord::ConnectionAdapters::Column ...>, ...]
Content.columns_hash # => {"id" => #<Column>, "title" => #<Column>, ...}
Content.attribute_names # => ["id", "title", "body", ...]
Content.table_name # => "contents"
Content.primary_key # => "id"
Content.table_exists? # => true/false
Content.content_columns # => all non-id, non-foreign-key columns
Content.attribute_types # => {"id" => integer type, ...}
Content.human_attribute_name(:created_at) # => "Created at"
Connection Management
For advanced use cases — multi-database apps, connection inspection:
Content.connection # current database connection
Content.connection_pool # the connection pool for this model
Content.connected? # is there an active connection?
Content.establish_connection(adapter: "postgresql", database: "archive")
Content.clear_active_connections!
Content.connection_config # => { adapter: "postgresql", host: "...", ... }
Inheritance and Reflection
Content.ancestors # => [Content, ApplicationRecord, ActiveRecord::Base, ...]
Content.superclass # => ApplicationRecord
Content.base_class # => Content (or parent if STI)
Content.subclasses # => direct subclasses
Content.descendants # => all descendant classes
Content.reflections # => { "comments" => has_many reflection, ... }
Content.reflect_on_association(:comments)
Content.reflect_on_all_associations(:has_many)
The Full Method List
When you run Content. in pry and see 591 possibilities, they break down roughly like this:
| Category | Examples | Count |
|---|---|---|
| Querying | find, where, order, joins, includes |
~40 |
| Callbacks | before_save, after_create, around_destroy |
~30 |
| Validations | validates, validates_presence_of |
~20 |
| Associations | has_many, belongs_to, accepts_nested_attributes_for |
~15 |
| Attributes | attribute, attr_readonly, column_names, attribute_types |
~30 |
| Schema | columns, table_name, primary_key, table_exists? |
~20 |
| Connection | connection, connection_pool, establish_connection |
~15 |
| Transactions | transaction, lock, uncached |
~10 |
| Ruby/Module | ancestors, include, extend, method_defined?, methods |
~400 |
The vast majority (400+) are standard Ruby Module, Object, and BasicObject methods that every class inherits — methods, respond_to?, is_a?, instance_variable_get, etc.
Comments