ActiveRecord CRUD Basics
Fast recall
Quick revision
Core answer
new builds without saving, create builds and attempts an INSERT, finders read rows, update changes attributes and attempts an UPDATE, and destroy removes a record while running destroy callbacks. Normal persistence methods run validations and return a result that must be checked; bang methods such as save! raise when persistence fails.Whether you can name the four CRUD operations and the ActiveRecord methods for each (create/save/find/where/update/destroy).
Using delete instead of destroy (delete skips callbacks), or not saving after new.
CRUD = Create/Read/Update/Delete; ActiveRecord maps them to create+save, find/where, update, destroy. destroy (not delete) runs callbacks.
Interview-ready guide
Interview answer
CRUD means Create, Read, Update, and Delete. Active Record maps those database operations to Ruby model objects and methods. The API is convenient, but each method still has a clear persistence, validation, callback, and failure contract that the caller must understand.
Article.new(...) builds an in-memory object and does not insert a row. save validates it and returns true or false; save! raises on validation failure. Article.create(...) combines building and saving but can still return an invalid, unpersisted object, so check the result rather than assuming success.
For reading, Article.find(id) returns the required primary-key record or raises RecordNotFound; find_by(...) returns one match or nil. update(...) validates and returns a Boolean, while update!(...) raises when the write is invalid. Pick the form that matches normal versus exceptional control flow.
A loaded record can call update(title: "Revised"), which assigns values, validates, runs callbacks, and attempts an UPDATE. destroy runs the destroy lifecycle and configured association behavior. delete removes directly and skips that lifecycle, so it belongs only in code that intentionally accepts those missing callbacks.
CRUD methods make one-record work readable; they do not make a multi-record business operation atomic by themselves. If an article and its audit row must both change or neither may change, wrap them in one database transaction and use a failure form that causes the transaction to roll back.
ActiveRecord CRUD Basics
Direct Answer
CRUD means Create, Read, Update, and Delete. Active Record maps those database operations to model objects and methods: new builds without saving, create builds and attempts an INSERT, finders read rows, update changes attributes and attempts an UPDATE, and destroy removes a record while running destroy callbacks. Normal persistence methods run validations and return a result that must be checked; bang methods such as save! raise when persistence fails.
Deep Dive & Architecture
An Active Record instance represents one row plus in-memory state. Before the first successful save it has no database identity. A successful INSERT assigns its primary key and clears tracked changes; later assignments remain in memory until another persistence method writes an UPDATE.
Validation and callback chains surround normal writes. A failed validation stops the SQL write but leaves the attempted values and error messages on the object. Bang and non-bang methods run the same model rules; they differ mainly in whether failure becomes an exception or a false return value.
Finder contracts matter at control-flow boundaries. find is appropriate when the record must exist and absence should take an exception path. find_by is appropriate when absence is a normal alternative the caller will branch on. Both still issue a query when evaluated.
Deletion has two meanings. Destroying asks the model to perform its lifecycle, including callbacks and configured dependent associations. Deleting issues a direct removal that bypasses that model lifecycle. The faster-looking method can leave related data or side effects inconsistent if the lifecycle was part of the contract.
Code Example
class Article < ApplicationRecord
validates :title, presence: true
end
article = Article.new(title: "Active Record basics")
if article.save
puts "created #{article.id}"
else
abort article.errors.full_messages.join(", ")
end
found = Article.find(article.id)
found.update!(published: true)
puts found.published?
found.destroy
puts found.destroyed?
Common Mistakes & Pitfalls
Using delete instead of destroy (delete skips callbacks), or not saving after new.
Check your understanding
Answer these without looking back at the article.
- What is the difference between `new`, `create`, and `create!`?
- When should a finder return `nil`, and when should it raise?
- Why can replacing `destroy` with `delete` change application behaviour?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.