Rails Encrypted Credentials: The Git Diff Feature You May Have Been Using Without Knowing

If you have been developing Rails applications for years, there’s a good chance you’ve used:

bin/rails credentials:edit

hundreds of times.

You probably know that Rails stores encrypted credentials in:

config/credentials.yml.enc

and keeps the encryption key separately in:

config/master.key

But did you know that Rails can make:

git diff

show the decrypted, human-readable changes to credentials.yml.enc?

I recently discovered this while working on a Rails 8.1.3.1 application and it was one of those:

“I’ve been using Rails every day for years, and I didn’t know Rails could do this!”

moments.

Let’s see how it works.


First: What is credentials.yml.enc?

Rails encrypted credentials allow us to keep secrets such as:

openai:
  api_key: ...

or:

aws:
  access_key_id: ...
  secret_access_key: ...

inside:

config/credentials.yml.enc

The file is encrypted.

The encryption key is stored separately in:

config/master.key

Rails documentation explicitly states that the encrypted credentials file can be stored in version control as long as the master key remains secure. (Ruby on Rails Guides)

So our repository can contain:

config/
├── credentials.yml.enc ← encrypted, safe to commit
└── master.key ← secret, NEVER commit

Editing Rails Credentials

Normally we edit credentials with:

bin/rails credentials:edit

Rails decrypts the credentials, opens them in your configured editor, and encrypts them again when you save.

Conceptually:

credentials.yml.enc
        │
        │ decrypt
        ▼
   Plain YAML
        │
        │ edit
        ▼
   Plain YAML
        │
        │ encrypt
        ▼
credentials.yml.enc

The plaintext credentials aren’t saved as a normal file.

But What Happens With git diff?

Here’s the interesting part.

If Git simply compared the encrypted files, we’d get something useless:

- 3d9Jx8...random-encrypted-data...
+ 7kP2mL...different-encrypted-data...

We wouldn’t know:

  • Which credential changed?
  • Was a key added?
  • Was a key removed?
  • Did the API key change?
  • Did somebody accidentally modify something?

This is where Rails’ credentials diff integration becomes useful.

Rails + Git textconv

Rails can configure Git to use a special diff driver:

[diff "rails_credentials"]
textconv = bin/rails credentials:diff

In my Rails 8.1 application, I found exactly this in:

.git/config

Git therefore doesn’t simply compare the encrypted contents.

Instead:

git diff
    │
    ▼
Git sees credentials.yml.enc
    │
    ▼
rails_credentials diff driver
    │
    ▼
bin/rails credentials:diff
    │
    ▼
Rails decrypts the credentials
    │
    ▼
Git displays a readable diff

Git itself doesn’t understand Rails encryption.

Rails is providing the text conversion command. Git simply knows how to invoke it.

See It Yourself

Suppose our credentials originally contain:

openai:
api_key: OLD_KEY

We change it to:

openai:
api_key: NEW_KEY

Now:

git diff

can show a useful diff such as:

+
+openai:
+ api_key: 'sdsdssdsdsdwewewddvcfgfgth'

That’s much more useful than comparing encrypted bytes.

The Experiment That Makes This Obvious

This is what made the behavior click for me.

Run:

git diff -- config/credentials.yml.enc

You get the human-readable credentials diff.

Now bypass Git’s text conversion:

git diff --no-textconv -- config/credentials.yml.enc

Now you see the encrypted content.

Something like:

3d9Jx8...encrypted-data...

That’s the proof.

The file itself is still encrypted.

It’s only the diff representation that’s being transformed.

So What Exactly Does Git Know?

Git doesn’t know anything about:

Rails
credentials
master.key
AES
encryption
decryption

Git knows:

diff driver
textconv

Rails configures:

rails_credentials

and tells Git:

When displaying a diff for this file,
run:
bin/rails credentials:diff

That’s a very nice example of two independent tools cooperating:

             Rails
               │
               │ provides
               ▼
       credentials:diff
               │
               ▼
             Git
               │
               │ uses
               ▼
           textconv

How Does Rails Configure It?

Rails provides:

bin/rails credentials:diff --enroll

This enrolls the project in credentials diffing.

The Git attributes include:

config/credentials/*.yml.enc diff=rails_credentials
config/credentials.yml.enc diff=rails_credentials

Rails then ensures the Git diff driver is configured to use:

bin/rails credentials:diff

Rails’ application generator includes this credentials diff enrollment as part of application setup and Rails 7.0 already contained the credentials diffing implementation. (Gem)

So this isn’t actually an 8.1-only feature.

That’s an important distinction.

Is This New in Rails 8.1?

No – and this is an important correction.

The encrypted credentials diff functionality existed before Rails 8.1.

For example, Rails 7.0 already had the credentials:diff implementation, and Rails 7.2’s application generator also enrolled projects in credentials diffing. (Gem)

Rails has supported decrypted Git diffs for encrypted credentials for several versions and Rails 8.x continues to build on the credentials tooling.

Rails 8.1 does introduce other useful credentials functionality. For example, Rails 8.1 added command-line credential fetching, which can be useful for deployment tooling such as Kamal. (Ruby on Rails Guides)

Does This Make My Secrets Unsafe?

No – provided you protect the master key.

The important distinction is:

Git repository
│
├── credentials.yml.enc
│       ↓
│   encrypted
│
└── master.key
        ↓
     SECRET

The encrypted file can be committed.

The master key should not be committed. Rails’ security guide explicitly recommends keeping the master key safe and out of version control. (Ruby on Rails Guides)

One Thing to Remember

The decrypted content can appear in your local terminal output.

For example:

git diff

could display:

+
+openai:
+  api_key: 'sdsdssdsdsdwewewddvcfgfgth'

So don’t casually share terminal screenshots containing credential diffs.

Also be careful when copying terminal output into:

  • Slack
  • GitHub issues
  • Pull requests
  • screenshots
  • blog posts
  • AI assistants

NOTE: The encryption protects the file stored in Git, but a decrypted diff is plaintext.

Rails Developer Takeaway

There are three different things here:

1. Encrypted file

config/credentials.yml.enc

This is what is actually stored in Git.

2. Encryption key

config/master.key

This decrypts the credentials and must remain secret.

3. Git diff representation

bin/rails credentials:diff

This is what allows us to see meaningful changes locally.

So:

                 GitHub
                   │
                   │ encrypted
                   ▼
       credentials.yml.enc
                   ▲
                   │
             master.key
             stays secret


Local git diff:

credentials.yml.enc
        │
        ▼
credentials:diff
        │
        ▼
decrypted representation
        │
        ▼
human-readable diff

Try This Yourself

If you’re working on a Rails application, check:

git config --show-origin --get-regexp 'diff|textconv|filter'

You may find:

file:.git/config diff.rails_credentials.textconv bin/rails credentials:diff

Then:

git diff --no-textconv -- config/credentials.yml.enc

Compare that with:

git diff -- config/credentials.yml.enc

The difference is a great way to understand what’s really happening.

Quick Reference

# Edit credentials
bin/rails credentials:edit

# Enroll project in credential diffing
bin/rails credentials:diff --enroll

# Normal readable diff
git diff

# Show the actual encrypted file diff
git diff --no-textconv -- config/credentials.yml.enc

# Inspect Git's configuration
git config --show-origin --get-regexp 'diff|textconv|filter'

# Check Git attributes
git check-attr diff -- config/credentials.yml.enc

Security rule:

 Y config/credentials.yml.enc → commit it
 X config/master.key          → NEVER commit it


Rails’ official security guide confirms that encrypted credentials can be stored in version control while the master key must remain protected. (Ruby on Rails Guides)

📚 References

Happy Coding!

Rails Database Transactions: A Senior Developer’s Guide to Atomicity, Rollbacks, Savepoints and Transaction-Safe Design

Database transactions are one of those Rails features that appear simple:

User.transaction do
  # database operations
end

But at a senior Rails engineering level, transactions are much more than wrapping a few save! calls in a block.

They affect data consistency, concurrency, failure handling, callbacks, database locking, nested service objects, multiple databases and even how external systems should be triggered.

A strong Rails developer should understand not only how to start a transaction, but also:

  • which transaction API to use
  • how class-level and instance-level transactions differ
  • what actually gets rolled back
  • how nested transactions work
  • when requires_new is necessary
  • how savepoints work
  • how save and destroy already use transactions internally
  • how after_commit differs from after_save
  • how to handle database exceptions safely
  • how transactions interact with locks and isolation levels
  • what Rails transactions cannot protect
  • how to design transaction boundaries in service objects

This article approaches transactions from that perspective.


What Is a Database Transaction?

A transaction groups multiple database operations into a single unit of work.

Conceptually:

BEGIN

operation 1
operation 2
operation 3

COMMIT

If something fails:

BEGIN

operation 1
operation 2
operation 3  <-- failure

ROLLBACK

The important property is atomicity:

Either all database changes become permanent, or none of them do.

Rails uses the database transaction facilities provided by the underlying database connection. Active Record describes transactions as protective blocks where SQL statements become permanent only when the complete operation succeeds. (Ruby on Rails API)

A classic example is transferring money:

Account.transaction do
  sender.withdraw!(100)
  receiver.deposit!(100)
end

We don’t want this situation:

sender: -100
receiver: 0

If the deposit fails, the withdrawal must also disappear.


The Basic Rails Transaction Block

The most common syntax is:

ActiveRecord::Base.transaction do
  user.save!
  profile.save!
  audit_log.save!
end

If any operation raises an exception, Rails rolls back the transaction. (Ruby on Rails API)

In a modern Rails application, however, I generally prefer:

ApplicationRecord.transaction do
  user.save!
  profile.save!
  audit_log.save!
end

Why?

ApplicationRecord represents the application’s Active Record hierarchy and is generally a clearer boundary than directly referencing ActiveRecord::Base.


Why Do We Need Transactions?

Imagine an order creation workflow:

order = Order.create!
payment = Payment.create!
order.update!(status: "paid")
InventoryItem.create!(...)

Without a transaction, failure halfway through could leave:

Order ✅
Payment ✅
Order paid ✅
Inventory ❌

The application is now inconsistent.

Instead:

ApplicationRecord.transaction do
  order = Order.create!
  payment = Payment.create!

  order.update!(status: "paid")

  InventoryItem.create!
end

Now the desired guarantee is:

Everything succeeds -> COMMIT
Anything fails -> ROLLBACK

Class-Level Transaction Methods

The class-level form is the most common Rails API.

User.transaction do
  user = User.create!
  profile = Profile.create!(user: user)
end

You can also use:

ApplicationRecord.transaction do
  user.update!
  profile.update!
end

An important senior-level detail is that the class calling .transaction doesn’t restrict which models can participate.

For example:

Account.transaction do
  account.update!(balance: 900)

  TransactionLog.create!(
    account: account,
    amount: 100
  )
end

TransactionLog participates in the same transaction.

Why?

Because a Rails transaction is fundamentally associated with a database connection, not with a particular model class. (Ruby on Rails API)

This is an important distinction:

Transaction
Database connection
SQL statements
Multiple ActiveRecord models

Not:

Transaction
One ActiveRecord model only

Instance-Level Transactions

Rails also supports transactions on model instances.

For example:

user.transaction do
user.update!(name: "Abhilash")
Profile.create!(user: user)
end

This might look like a different mechanism from:

User.transaction do
user.update!(name: "Abhilash")
Profile.create!(user: user)
end

For normal Active Record usage, they operate against the same underlying database connection.

Rails explicitly provides transaction as both a class-level and model-instance API. (Ruby on Rails API)

When is instance-level useful?

It can communicate intent:

order.transaction do
order.update!(status: "processing")
order.create_payment!
end

This reads naturally as:

Perform these operations as one transaction around this order.

However, I would usually use the class-level transaction in service objects, because the service boundary is about a unit of work rather than a specific model.


Service Object Transaction Boundary

This is usually my preferred architecture for complex workflows.

class CheckoutService
  def call(user:, cart:)
    Order.transaction do
      order = create_order(user, cart)
      charge_payment(order)
      reserve_inventory(order)

      order.update!(status: "confirmed")

      order
    end
  end

  private

  def create_order(user, cart)
    Order.create!(
      user: user,
      total: cart.total
    )
  end

  def charge_payment(order)
    Payment.create!(
      order: order,
      amount: order.total
    )
  end

  def reserve_inventory(order)
    # ...
  end
end

This gives the workflow one explicit transaction boundary.

That is much easier to reason about than having every individual method start its own transaction.


save and destroy Are Already Transactional

This is an area that surprises many developers.

Rails automatically wraps save and destroy operations in transactions. This ensures validations and callbacks execute under transactional protection. (Ruby on Rails API)

For example:

user.save!

is already transaction-protected at the database-operation level.

But that doesn’t mean you don’t need explicit transactions.

Consider:

user.save!
profile.save!

Each operation is protected individually.

That does not give you:

user + profile = atomic unit

You need:

User.transaction do
user.save!
profile.save!
end

So the distinction is:

save!
protect this persistence operation
transaction do
protect this entire workflow

Transaction and Exceptions

The normal rollback mechanism is an exception.

User.transaction do
user.save!
profile.save!
raise "Something failed"
end

The transaction rolls back.

Rails then propagates the exception to the caller.

That means this pattern is common:

begin
User.transaction do
create_user!
create_profile!
end
rescue StandardError => e
Rails.logger.error(e.message)
raise
end

The important design principle is:

Don’t use transactions as a replacement for error handling.

A transaction determines what happens to database state.

Your application code still needs to determine what happens to the failure.


ActiveRecord::Rollback

Rails provides a special exception:

ActiveRecord::Rollback

Example:

User.transaction do
user.update!(status: "processing")
unless payment_valid?
raise ActiveRecord::Rollback
end
user.update!(status: "confirmed")
end

The transaction rolls back, but ActiveRecord::Rollback is specifically handled by Rails and isn’t propagated like a normal exception. (Ruby on Rails API)

This makes it useful when you intentionally want:

rollback database changes
+
don't treat this as an application exception

For example:

Order.transaction do
order.update!(status: "processing")
raise ActiveRecord::Rollback unless inventory_available?
end

raise vs ActiveRecord::Rollback

Compare:

raise PaymentError

with:

raise ActiveRecord::Rollback

The semantic difference is significant.

Normal exception

raise PaymentError

Result:

ROLLBACK
exception propagates
caller can rescue it

ActiveRecord::Rollback

raise ActiveRecord::Rollback

Result:

ROLLBACK
rollback is internally handled
execution exits transaction

Therefore, don’t blindly replace business exceptions with ActiveRecord::Rollback.


Nested Transactions

Consider:

ApplicationRecord.transaction do
user.save!
ApplicationRecord.transaction do
profile.save!
end
end

You might assume there are two independent transactions:

BEGIN
user
BEGIN
profile
COMMIT
COMMIT

That’s not generally how Rails works.

By default, a nested transaction joins the existing transaction. Most databases don’t provide true nested transactions, so Rails emulates subtransactions with savepoints where necessary. (Ruby on Rails API)

Conceptually:

BEGIN
user
profile
COMMIT

The Nested Rollback Surprise

Consider this:

User.transaction do
User.create!(name: "A")
User.transaction do
User.create!(name: "B")
raise ActiveRecord::Rollback
end
end

Many developers expect:

A -> committed
B -> rolled back

But because the nested transaction joins the parent transaction, the rollback exception is handled by the inner transaction boundary and the outer transaction can still commit.

The result can be:

A -> committed
B -> committed

This behavior is documented by Rails and is one of the most important transaction gotchas to understand. (Ruby on Rails API)

requires_new: true

When you need an actual nested transactional boundary, use:

requires_new: true

Example:

ApplicationRecord.transaction do
user = User.create!
ApplicationRecord.transaction(requires_new: true) do
AuditLog.create!
raise ActiveRecord::Rollback
end
end

Now the inner transaction gets its own savepoint.

Conceptually:

BEGIN
User
SAVEPOINT
AuditLog
ROLLBACK TO SAVEPOINT
COMMIT

Result:

User ✅
AuditLog ❌

Rails uses database save points to emulate nested transactions on databases that do not support true nested transactions.


When Should You Use requires_new?

It is especially useful when you have an inner operation that should be isolated from the outer workflow.

For example:

Order.transaction do
create_order!
Order.transaction(requires_new: true) do
create_optional_audit_record!
end
finalize_order!
end

The inner operation can fail without necessarily destroying the outer work.

This is particularly useful in reusable service objects.

Suppose:

class AuditService
def self.record!(event)
AuditLog.transaction(requires_new: true) do
AuditLog.create!(event: event)
end
end
end

That service can be called either:

AuditService.record!("user_created")

or inside another transaction:

User.transaction do
user.save!
AuditService.record!("user_created")
end

The requires_new boundary gives the service an explicit savepoint when called inside an existing transaction.


Transaction Isolation Levels

Transactions don’t only provide atomicity.

They also influence how concurrent transactions can see and modify data.

Rails allows:

Account.transaction(isolation: :serializable) do
# critical operation
end

Supported isolation levels include:

:read_uncommitted
:read_committed
:repeatable_read
:serializable

Support and semantics depend on the database adapter. Rails documents these options and notes that isolation cannot generally be changed while joining an existing transaction or creating a nested savepoint transaction.

For example:

Order.transaction(isolation: :serializable) do
order = Order.find(order_id)
order.update!(
status: "confirmed"
)
end

This can be appropriate for highly concurrent business operations, but it isn’t something I would enable casually.

Higher isolation can increase contention and retry requirements.

A senior engineer should ask:

What consistency guarantee does this business operation actually require?

rather than:

Which isolation level sounds safest?


Transactions + Row Locking

Transactions become particularly powerful when combined with pessimistic locking.

Suppose two requests attempt to update the same account simultaneously.

account.with_lock do
account.update!(
balance: account.balance - 100
)
end

with_lock is a useful Rails shortcut: it starts a transaction, reloads the record using a row lock, and then executes the block. Rails also allows transaction options such as requires_new, isolation, and joinable to be passed to with_lock. (Ruby on Rails API)

Conceptually:

BEGIN
SELECT ... FOR UPDATE
modify row
COMMIT

This is useful for operations such as:

account.with_lock do
raise InsufficientFunds unless account.balance >= amount
account.update!(
balance: account.balance - amount
)
end

The important point is that transaction + locking solves a different class of problem from transaction alone.

A transaction provides atomicity.

A lock helps control concurrent access.

with_lock vs transaction

Compare:

account.transaction do
account.update!(balance: ...)
end

with:

account.with_lock do
account.update!(balance: ...)
end

The first provides transactional atomicity.

The second provides:

transaction
+
record reload
+
row-level locking

Use with_lock when concurrency around a particular row is part of the problem.

after_save vs after_commit

This distinction becomes extremely important when transactions are involved.

Consider:

class Order < ApplicationRecord
after_save :publish_order
def publish_order
EventBus.publish(id)
end
end

Suppose:

Order.transaction do
order.update!(status: "paid")
raise "Something failed"
end

The after_save callback can run while the transaction is still in progress.

The database transaction can subsequently roll back.

Now your external system might have received:

Order paid

while your database says:

Order unpaid

That’s dangerous.


Use after_commit for External Side Effects

Rails provides:

class Order < ApplicationRecord
after_commit :publish_order
private
def publish_order
EventBus.publish(id)
end
end

Now the external operation happens only after the database transaction has successfully committed.

Rails explicitly recommends transaction callbacks such as after_commit when interacting with systems outside the database transaction. (Ruby on Rails Guides)

For narrower cases:

after_create_commit :publish_order
after_update_commit :publish_order
after_destroy_commit :remove_from_search

These are convenient aliases provided by Rails.


Per-Transaction Callbacks

Modern Rails also allows callbacks to be registered directly against a transaction.

For example:

Order.transaction do |transaction|
order.update!(status: "confirmed")
transaction.after_commit do
NotificationService.notify_order_confirmed(order)
end
end

This is interesting because the callback is associated with the unit of work, rather than with the model lifecycle.

Rails supports transaction-level callbacks such as:

transaction.before_commit
transaction.after_commit
transaction.after_rollback

This can be cleaner for domain/service-oriented workflows where you don’t want the model itself to know about notification behavior.


ActiveRecord.after_all_transactions_commit

Another useful modern Rails API is:

ActiveRecord.after_all_transactions_commit do
NotificationService.notify(...)
end

This is useful when code may be invoked from either inside or outside a transaction.

Rails guarantees that the callback runs after all currently open transactions have successfully committed. If any transaction rolls back, the callback isn’t executed.

This can be particularly useful in reusable application services.


Article.current_transaction

Modern Rails exposes transaction state through:

Article.current_transaction

You can register an operation:

Article.current_transaction.after_commit do
SearchIndexer.index(article)
end

This makes a service transaction-aware without requiring it to know whether its caller has opened a transaction.

Rails documents this API as a representation of the current transaction, savepoint, or lack of an active transaction. (Ruby on Rails API)

This is particularly interesting for reusable service objects.

For example:

class PublishArticle
def self.call(article)
article.update!(published: true)
Article.current_transaction.after_commit do
SearchIndexer.index(article)
end
end
end

Now:

PublishArticle.call(article)

works both:

outside transaction

and:

Article.transaction do
PublishArticle.call(article)
end

The external action can correctly follow the transaction boundary.


Don’t Rescue StatementInvalid Inside a Transaction

This is one of the most important PostgreSQL-specific transaction rules.

Bad:

User.transaction do
begin
User.create!(email: "existing@example.com")
rescue ActiveRecord::StatementInvalid
# ignore
end
User.create!(email: "new@example.com")
end

A database error such as a unique constraint violation can leave the PostgreSQL transaction in an aborted state.

After that, subsequent SQL statements can fail with an error similar to:

current transaction is aborted,
commands ignored until end of transaction block

Rails explicitly recommends restarting the entire transaction after ActiveRecord::StatementInvalid, rather than continuing within the damaged transaction.

Better:

begin
User.transaction do
create_user!
create_profile!
end
rescue ActiveRecord::RecordNotUnique
# retry or handle outside the transaction
end

The key idea is:

Database failure
Transaction may be unusable
Exit transaction
Handle/retry outside it

This is especially important when building retry logic for concurrency errors.


Transactions Are Not Distributed Transactions

A Rails transaction normally operates on one database connection.

Therefore:

User.transaction do
user.save!
AuditLog.create!
end

works when those models participate in the same database connection.

But imagine:

Primary DB
User
Analytics DB
AnalyticsEvent

A transaction on the primary database cannot automatically roll back a transaction on another database connection.

Rails explicitly documents that transactions are not distributed across database connections.

This becomes especially important with Rails multiple-database applications.


Multiple Databases: Don’t Assume One Transaction

Imagine:

User.transaction do
user.update!
AnalyticsEvent.transaction do
analytics_event.save!
end
end

These are potentially separate database transactions.

You don’t suddenly have:

BEGIN DB1
BEGIN DB2
COMMIT DB1
COMMIT DB2

with a globally atomic guarantee.

Instead, you have two independent database resources.

This is where architectural patterns such as:

  • transactional outbox
  • event-driven processing
  • retries
  • idempotency
  • compensating actions

become more appropriate than trying to force a distributed transaction.


Transactions and Background Jobs

Consider:

Order.transaction do
order.update!(status: "confirmed")
OrderConfirmationJob.perform_later(order.id)
end

This can be dangerous.

Depending on timing, the job could execute before the surrounding transaction has committed.

Then the worker might query:

Order.find(order_id)

and not observe the expected committed state.

Instead:

Order.transaction do
order.update!(status: "confirmed")
order.after_commit do
OrderConfirmationJob.perform_later(order.id)
end
end

Or use the appropriate transactional callback mechanisms.

The principle is:

Don’t allow asynchronous consumers to depend on database state that hasn’t committed yet.

Rails’ transaction callbacks are specifically designed for such post-commit work.


Keep Transactions Small

A transaction should generally cover the minimum amount of work necessary.

Avoid:

Order.transaction do
order.update!
HTTP.get(payment_api)
HTTP.get(shipping_api)
expensive_calculation
sleep(5)
order.update!
end

Now the database transaction stays open while waiting on external systems.

That can mean:

transaction open
database connection occupied
locks potentially held
other requests wait
throughput decreases

A better architecture is often:

external preparation
short DB transaction
commit
after_commit / job
external side effect

Transaction Boundary vs Business Operation

A useful senior-level rule is:

A transaction boundary should normally correspond to a business operation that must be atomic.

For example:

Order.transaction do
create_order!
reserve_inventory!
record_payment!
end

That’s a meaningful transaction.

But this:

User.transaction do
user.update!
end

may be unnecessary if you’re only performing one persistence operation.

Remember:

user.update!

already has transactional protection around the persistence operation.


Testing Transaction Behavior

Transactions are especially valuable to test explicitly.

Example:

it "rolls back the order when payment fails" do
expect {
CheckoutService.call(user, cart)
}.to raise_error(PaymentError)
expect(Order.count).to eq(0)
end

Test the business guarantee, not the implementation detail.

Good transaction tests answer questions such as:

Does failed payment rollback the order?
Does failed inventory reservation rollback the payment?
Does an after_commit job run only after successful commit?
Does a nested requires_new operation rollback independently?

A Practical Senior-Level Example

Let’s build a realistic checkout flow.

class CheckoutService
  def call(user:, cart:)
    order = nil

    Order.transaction do
      order = Order.create!(
        user: user,
        total: cart.total,
        status: "pending"
      )

      reserve_inventory!(cart)

      Payment.create!(
        order: order,
        amount: cart.total,
        status: "paid"
      )

      order.update!(status: "confirmed")

      ActiveRecord::after_all_transactions_commit do
        OrderConfirmationJob.perform_later(order.id)
      end
    end

    order
  end

  private

  def reserve_inventory!(cart)
    cart.items.each do |item|
      item.product.with_lock do
        raise OutOfStock if item.product.stock < item.quantity

        item.product.update!(
          stock: item.product.stock - item.quantity
        )
      end
    end
  end
end

There are several senior-level ideas here.

Atomicity

Order.transaction

ensures the order, payment and inventory changes form one unit.

Concurrency control

with_lock

protects inventory from concurrent updates.

Post-commit processing

ActiveRecord.after_all_transactions_commit

prevents the job from being dispatched before the transaction chain is complete.

This is much closer to production-grade transaction design than simply knowing:

Model.transaction do
end

Transaction APIs at a Glance

APIMain purposeTypical usage
Model.transactionTransaction around a unit of workService objects
instance.transactionTransaction associated with a model instanceModel-centric workflows
ApplicationRecord.transactionApplication-wide transaction boundaryShared models
transaction(requires_new: true)Independent nested savepointIsolating sub-operations
transaction(isolation: :serializable)Stronger concurrency guaranteesHighly concurrent workflows
with_lockTransaction + row lockBalance/inventory updates
after_commitRun code after commitExternal side effects
after_rollbackReact to rollbackCleanup/recovery logic
transaction.after_commitCallback attached to a specific transactionService/domain workflows
ActiveRecord.after_all_transactions_commitRun after outermost transaction chain commitsTransaction-aware reusable services
current_transaction.after_commitMake services transaction-awareReusable domain services

Rails provides all of these around the same fundamental transaction system.


How I Decide Which API to Use

As a practical decision tree:

One database operation

Usually:

user.update!

No explicit transaction required.

Several operations must succeed together

Use:

User.transaction do
...
end

Reusable service may be called inside another transaction

Consider:

transaction(requires_new: true)

when independent rollback semantics are actually required.

Concurrent modification of one row

Use:

record.with_lock do
...
end

External system must run only after DB success

Use:

after_commit

or a transaction-aware post-commit mechanism.

Multiple database connections

Don’t assume a single transaction protects everything.

Consider:

outbox
events
idempotency
retries
compensating actions

instead.


Common Transaction Mistakes

1. Putting HTTP calls inside transactions

Order.transaction do
order.save!
PaymentGateway.charge!
end

Avoid long-running external calls inside database transactions.

2. Assuming nested transactions are independent

transaction do
transaction do
end
end

The inner block normally participates in the outer transaction.

Use:

transaction(requires_new: true)

when you specifically need savepoint-based isolation.

3. Publishing events from after_save

Bad:

after_save :publish_event

for external systems that require committed data.

Prefer:

after_commit :publish_event

4. Rescuing database errors inside the transaction

Bad:

transaction do
begin
risky_database_operation
rescue ActiveRecord::StatementInvalid
end
another_database_operation
end

For PostgreSQL in particular, leave the failed transaction and retry/recover at a higher level.

5. Assuming transactions protect in-memory Ruby objects

Suppose:

user = User.find(1)
User.transaction do
user.update!(name: "New Name")
raise ActiveRecord::Rollback
end

The database row is rolled back.

But don’t assume your Ruby object has magically reverted every piece of in-memory state to its pre-transaction state. Rails explicitly notes that database rollback doesn’t restore Active Record objects to their original in-memory state.


Rails Takeaways

The important lesson isn’t:

“Use .transaction when you have multiple saves.”

The deeper mental model is:

                 Transaction

┌───────────┴───────────┐
│ │
Atomicity Concurrency
│ │
commit/rollback locks/isolation
│ │
└───────────┬───────────┘

Application
boundary

┌───────────┴────────────┐
│ │
DB operations external systems
│ │
transaction after_commit

A senior Rails developer should decide transaction boundaries deliberately.

The questions I would ask during a code review are:

What exactly must be atomic?
Which database connection is involved?
Can this service be called inside another transaction?
Does this nested transaction really need requires_new?
Are we holding locks longer than necessary?
Could a database constraint failure leave the transaction unusable?
Are we calling an external service before commit?
Can a background job observe uncommitted data?
Are multiple databases involved?
Do we need a lock, or is a transaction alone sufficient?
What happens when this operation is executed concurrently?

That is where transaction knowledge moves from Rails syntax to system design.


Final Mental Model

Think about Rails transactions in five layers:

1. transaction
Atomic unit of database work
2. rollback
Undo database changes when work fails
3. requires_new / savepoints
Isolate nested database work
4. locks / isolation
Control concurrent behavior
5. after_commit
Safely interact with the world outside the DB

Mastering these five concepts gives you most of what you need to design transaction-safe Rails applications.

And the biggest senior-level principle is simple:

Transactions should protect business invariants, not merely surround database code.

That distinction is what separates knowing the Rails transaction API from designing reliable transactional systems.

References

  • Rails Active Record Transactions API and transaction semantics. (Ruby on Rails API)
  • Rails transaction callbacks, after_commit, after_rollback, per-transaction callbacks, and after_all_transactions_commit. (Ruby on Rails Guides)
  • Rails ActiveRecord::Transaction API, including transaction-aware callbacks and current_transaction. (Ruby on Rails API)
  • Rails pessimistic locking and with_lock. (Ruby on Rails API)

Happy Implementing!

Ruby’s Mysterious Symbols: The Syntax Every Ruby Developer Should Truly Understand

Ruby is famous for making code expressive.

But that expressiveness comes with a side effect: Ruby contains quite a few symbols and syntax constructs that can look almost cryptic – even to experienced developers coming from other languages.

Consider this:

message = <<~TEXT
  Hello #{user.name},

  Your order has been shipped.

  Thanks!
TEXT

What exactly does <<~TEXT mean?

Or:

users.filter_map { _1.email if _1.active? }

What is _1?

Or:

case response
in { status: 200, body: String => body }
  puts body
end

Why does Ruby allow String =>>>> body inside a pattern?

These aren’t random pieces of syntax. They are examples of Ruby’s philosophy: make common programming operations concise without sacrificing readability.

This article explores some of Ruby 3.4’s most interesting “mysterious” syntax and more importantly explains what each construct means, why it exists, and when a senior developer should use it – or avoid it.


1. <<~ – The Squiggly Heredoc

Let’s start with one of the most useful Ruby syntax features.

message = <<~TEXT
  Hello World
    This is Ruby
  Goodbye
TEXT

The <<~ syntax is called a squiggly heredoc.

What is a heredoc?

A heredoc allows you to define a multiline string:

message = <<TEXT
Hello
World
TEXT

Ruby keeps the newlines inside the string.

The problem is indentation.

In real Ruby code, especially Rails applications, multiline strings are usually nested inside methods, classes, conditionals, etc.

Without squiggly heredoc:

def email_body
  <<TEXT
Hello,
Welcome to our application.
Thank you.
TEXT
end

The heredoc terminator often needs awkward indentation.

<<~ solves that

def email_body
  <<~TEXT
    Hello,
    Welcome to our application.
    Thank you.
  TEXT
end

Ruby removes the common leading indentation.

Conceptually:

source indentation
        ↓
    Hello
    Welcome
    Thank you

becomes:

Hello
Welcome
Thank you

Why is this useful in Rails?

Extremely useful for SQL:

sql = <<~SQL
  SELECT users.*
  FROM users
  INNER JOIN orders ON orders.user_id = users.id
  WHERE users.active = TRUE
SQL

Or HTML:

html = <<~HTML
  <div class="user">
    <h2>#{user.name}</h2>
  </div>
HTML

Or shell commands:

command = <<~BASH
  echo "Starting deployment"
  bundle exec rails db:migrate
  echo "Deployment complete"
BASH

The senior-level takeaway

<<~ isn’t merely a formatting convenience.

It lets the Ruby source code remain properly indented without contaminating the resulting string with that indentation.


2. <<- vs <<~ vs <<

Ruby actually has several heredoc variants.

<<TEXT
...
TEXT

Strict terminator placement.

<<-TEXT
...
  TEXT

Allows the terminator to be indented.

<<~TEXT
...
  TEXT

Allows indentation and removes common indentation from the resulting string.

So in modern Ruby code, <<~ is generally the most readable choice for indented multiline strings.

Read more here: https://railsdrop.com/ruby-more-about-ruby-hearedoc-questions-and-answers/


3. %i[...] – Creating Arrays of Symbols

This:

%i[admin editor viewer]

creates:

[:admin, :editor, :viewer]

Similarly:

%w[admin editor viewer]

creates:

["admin", "editor", "viewer"]

The % syntax is Ruby’s percent literal syntax.

Common forms

%w[one two three]     # strings
%i[one two three]     # symbols
%W[hello #{name}]     # interpolated strings
%I[hello #{name}]     # interpolated symbols

This:

%i[read write delete]

is often cleaner than:

[:read, :write, :delete]

Especially when the list becomes long:

ALLOWED_ROLES = %i[
  admin
  manager
  editor
  viewer
].freeze

Read more here: https://railsdrop.com/ruby-more-about-rubys-percent-literal-syntax/


4. &. – The Safe Navigation Operator

One of the most recognizable Ruby operators:

user&.profile&.address&.city

It means:

Call the next method only if the receiver isn’t nil.

Instead of:

if user
  if user.profile
    if user.profile.address
      user.profile.address.city
    end
  end
end

Ruby lets you write:

user&.profile&.address&.city

But don’t blindly use it

This is an important senior-level distinction.

If the business logic says:

A user must have a profile.

then this:

user&.profile&.address

may hide a data integrity problem.

Sometimes you actually want:

user.profile.address

so that invalid state fails loudly.

Good use

Optional data:

current_user&.avatar&.url

Potentially bad use

Required relationships:

order&.customer&.account&.billing_address

If all those associations are supposed to exist, safe navigation may simply hide broken application state.

Use &. when nil is genuinely expected – not merely because it prevents exceptions.


5. &:method – Symbol-to-Proc Conversion

You’ve probably seen:

users.map(&:email)

It looks strange initially.

It’s effectively shorthand for:

users.map { |user| user.email }

Ruby converts:

:email

into a callable block using &.

So:

users.map(&:email)

is approximately:

users.map { |user| user.email }

Another example

numbers.select(&:even?)

is equivalent to:

numbers.select { |number| number.even? }

Important distinction

These are not the same:

users.map(:email)

and:

users.map(&:email)

The & tells Ruby:

Convert this object into a Proc and pass it as the block.


6. _1, _2, _3 – Numbered Parameters

Modern Ruby provides implicit block parameters.

Instead of:

users.map { |user| user.email }

you can write:

users.map { _1.email }

_1 means:

The first block argument.

Similarly:

array.map { |value, index| ... }

can conceptually be accessed using:

_1
_2

For example:

[10, 20, 30].map { _1 * 2 }

produces:

[20, 40, 60]

Where it works well

Small transformations:

users.map { _1.email }
orders.select { _1.total >> 1000 }
names.map { _1.upcase }

Where it becomes bad

Complex blocks:

users.map { _1.orders.select { _2.paid? }.map { _1.total } }

At this point, explicit names are much easier to understand.

users.map do |user|
user.orders.select { |order| order.paid? }
.map { |order| order.total }
end

Senior Ruby code optimizes for comprehension, not character count.


7. ... – The Argument Forwarding Operator

Ruby’s ... is particularly useful when wrapping methods.

Consider:

def log(*args, **kwargs, &block)
puts "Calling method"
super
end

Modern Ruby allows forwarding arguments directly:

def log(...)
puts "Calling method"
super
end

The ... means:

Forward all positional arguments, keyword arguments, and the block.

For example:

def instrument(...)
  start = Process.clock_gettime(Process::CLOCK_MONOTONIC)

  result = super

  duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
  puts "Took #{duration}s"

  result
end

This is particularly valuable for decorators, wrappers, instrumentation and delegation.


8. * – The Splat Operator

Ruby’s * has several important meanings.

Array expansion

numbers = [1, 2, 3]
puts(*numbers)

is effectively:

puts(1, 2, 3)

Collecting arguments

def sum(*numbers)
numbers.sum
end

Now:

sum(1, 2, 3, 4)

works because numbers becomes:

[1, 2, 3, 4]

Array destructuring

first, *middle, last = [1, 2, 3, 4, 5]

results in:

first # 1
middle # [2, 3, 4]
last # 5

This makes * one of Ruby’s most versatile operators.


9. ** – Keyword Argument Splat

The double splat is the keyword-argument equivalent.

options = {
timeout: 10,
retries: 3
}
client.call(**options)

This expands the hash into keyword arguments.

And:

def connect(**options)
options
end

collects arbitrary keyword arguments.

connect(timeout: 10, retries: 3)

gives:

{
timeout: 10,
retries: 3
}

This becomes particularly important when building APIs, service objects and forwarding methods in modern Ruby.


10. =>>>> Is More Than Hash Syntax

Most Ruby developers first encounter:

{ name: "Abhilash" }

But =>>>> has several meanings.

Hash rockets

{ "name" =>> "Abhilash" }

Pattern matching

Ruby pattern matching also uses =>>>>.

case response
in { status: 200, body: String =>> body }
puts body
end

Here:

String =>> body

means roughly:

Match a String and bind the matched value to body.

This is part of Ruby’s increasingly powerful pattern matching system.


11. Ruby Pattern Matching with in

Ruby’s case statement can do structural matching.

case user
in { name:, role: "admin" }
  puts "#{name} is an admin"
else
  puts "Not an admin"
end

The pattern:

{ name:, role: "admin" }

means:

  • the object should have a name
  • role must equal "admin"
  • bind the name value to the local variable name

This is considerably more powerful than a traditional case comparison.

Array patterns

case coordinates
in [x, y]
  puts "Point: #{x}, #{y}"
end

Why senior developers should care

Pattern matching becomes useful when processing:

  • API responses
  • parsed JSON
  • AST structures
  • event payloads
  • command results
  • structured domain objects

Instead of writing nested conditionals, you can express the expected structure directly.


12. in vs if

Traditional Ruby:

if response.is_a?(Hash) &&
   response[:status] == 200
  ...
end

Pattern matching:

case response
in { status: 200 }
  ...
end

The second version communicates the shape of the data rather than manually checking each property.

That is the deeper value of pattern matching.


13. | – Destructuring and Pattern Alternatives

Ruby’s | isn’t only the bitwise OR operator.

In pattern matching:

case value
in 1 | 2 | 3
puts "Small number"
end

means:

Match 1 OR 2 OR 3.

This makes pattern matching expressive:

case status
in 200 | 201 | 204
puts "Success"
in 400 | 401 | 403
puts "Client error"
end

14. =>>>> in Pattern Matching Can Bind Values

Consider:

case result
in Integer =>> value
puts value
end

This performs a type match and binds the value.

For example:

result = 42

matches:

Integer =>> value

and:

value
# =>> 42

This becomes powerful when patterns become more complex.


15. ... in Ranges

Ruby’s range syntax has two forms:

1..10

and:

1...10

The difference:

1..10

includes 10.

1...10

excludes 10.

Therefore:

(1..10).to_a

gives:

[1,2,3,4,5,6,7,8,9,10]

while:

(1...10).to_a

gives:

[1,2,3,4,5,6,7,8,9]

This is especially useful for array slicing:

numbers[0...3]

returns the first three elements.


16. .. Can Be Used in Conditions

Ruby has another interesting use of ranges.

case number
when 1..10
  puts "Small"
when 11..100
  puts "Medium"
end

This is one reason Ruby ranges are more than simply “start/end values.”


17. =>>>> vs : in Hashes

These are both valid:

{ name: "Ruby" }

and:

{ :name =>> "Ruby" }

But modern Ruby generally prefers:

{ name: "Ruby" }

The hash rocket remains useful when keys aren’t symbols:

{
"Content-Type" =>> "application/json",
"X-Request-ID" =>> request_id
}

This is a good example of Ruby syntax evolving toward readability while retaining backwards compatibility.


18. ? and ! Are Part of Ruby’s API Design

Ruby method names can end with ?:

user.active?

This convention means:

The method answers a yes/no question.

Examples:

empty?
nil?
valid?
persisted?
published?

The ! convention usually communicates:

This method performs a more dangerous, mutating, or exceptional version of an operation.

Examples:

save!
update!
destroy!
compact!

But an important senior-level detail:

Ruby does not enforce the semantic meaning of !.

You can technically write:

def hello!
"hello"
end

The meaning is a convention established by Ruby developers.


19. :: – Constant Lookup and Method Calls

Most developers know:

User::NAME

But :: can also invoke methods:

object::method

although the . form is overwhelmingly more idiomatic for method calls.

The primary modern use is constant/module navigation:

ActiveRecord::Base
Rails::Application
JSON::ParserError

It communicates namespace traversal.


20. @, @@ and $

Ruby has several variable scopes represented visually.

Local variable

name = "Ruby"

Instance variable

@name = "Ruby"

belongs to an object instance.

Class variable

@@name = "Ruby"

is shared across a class hierarchy.

Global variable

$name = "Ruby"

is globally accessible.

From a senior Rails perspective:

Prefer local and instance variables. Be extremely cautious with class variables and globals.

For example, Rails applications rarely need:

@@configuration

or:

$global_state

because they introduce difficult-to-control shared state.


21. ||= – Lazy Initialization

This is everywhere in Ruby:

@client ||= Client.new

It means roughly:

@client = @client || Client.new

If @client is already truthy, Ruby keeps it.

Otherwise, it creates the object.

This is commonly used for memoization:

def expensive_service
@expensive_service ||= ExpensiveService.new
end

But remember

||= checks truthiness, not whether the variable has ever been assigned.

So if:

@value = false

then:

@value ||= calculate_value

will call calculate_value.

That distinction matters when memoizing boolean values.


22. &&= and ||= Are Assignment Operators

Ruby also supports:

value &&= other

and:

value ||= other

For example:

user.active &&= user.verified?

means approximately:

user.active = user.active && user.verified?

These are concise, but they should be used only when the resulting expression remains obvious.


23. +=, -=, *=, /=

Ruby supports compound assignment:

counter += 1

Conceptually:

counter = counter + 1

For object attributes:

user.score += 10

is conceptually equivalent to:

user.score = user.score + 10

Ruby’s expressive assignment syntax is one of the reasons its code can remain compact without introducing a separate statement syntax.


24. defined? – Ask Ruby Whether Something Exists

Ruby provides:

defined?(variable)

For example:

defined?(@user)

may return:

"instance-variable"

You can also inspect constants:

defined?(Rails)

This can be useful for metaprogramming and conditional loading, although it should not be used as a substitute for proper application design.


25. respond_to? – Duck Typing in Action

Ruby’s duck typing philosophy often appears as:

object.respond_to?(:call)

Instead of asking:

object.is_a?(SomeSpecificClass)

you ask:

Can this object perform the operation I need?

For example:

if logger.respond_to?(:info)
logger.info("Processing started")
end

This is particularly useful when designing flexible Ruby APIs.


26. method(:foo) – Turn a Method into an Object

Ruby treats methods as objects through Method:

method = user.method(:email)

Then:

method.call

invokes it.

This is useful in metaprogramming and dynamic dispatch.

For example:

operation = object.method(:calculate)
operation.call

Ruby’s object model makes this possible without requiring a separate function-pointer concept.


27. public_send vs send

Ruby allows dynamic method invocation:

user.send(:email)

But send can invoke private methods.

For user-controlled or externally supplied method names, this can be dangerous.

Prefer:

user.public_send(:email)

when you intentionally want to restrict invocation to public methods.

This distinction becomes important when building generic service layers or DSLs.


28. then / yield_self – Pipeline-Style Ruby

Ruby provides:

object.then do |value|
...
end

For example:

result =
User.new
.then { |user| user.save! }
.then { |user| user.email }

then passes the receiver into the block and returns the block’s result.

It can be useful when constructing transformations without introducing temporary variables.

But don’t turn everything into a pipeline merely because Ruby allows it.


29. _ – The Intentionally Ignored Variable

You’ll frequently see:

users.each do |user, _index|
puts user.name
end

The _ communicates:

This value exists, but I intentionally don’t care about it.

Ruby also allows:

_ = expensive_result

although explicit naming is generally preferable unless you’re intentionally ignoring something.


30. Endless Method Definitions

Ruby allows:

def full_name = "#{first_name} #{last_name}"

instead of:

def full_name
"#{first_name} #{last_name}"
end

This is called an endless method definition.

It’s excellent for very small methods:

def active? = status == "active"
def total = price * quantity

But don’t use it for complex logic.

This:

def process = validate && save && notify && publish

may be syntactically elegant but is much harder to maintain.


31. =>>>> – Rightward Assignment

Modern Ruby also supports rightward assignment:

value =>> variable

For example:

"hello" =>> message

Now:

message
# =>> "hello"

This becomes particularly interesting with pattern matching:

response =>> { status:, body: }

It allows destructuring and binding in a visually different direction.

The feature is useful, but like many Ruby syntactic conveniences, it should be used when it improves readability—not simply because it is available.


32. The Bigger Picture: Ruby Syntax Is a Language of Intent

After seeing all these operators, it is tempting to memorize them as a collection of Ruby tricks.

That would miss the important point.

Ruby’s syntax frequently tries to encode intent.

Compare:

users.map { |user| user.email }

with:

users.map(&:email)

The second says:

Transform each user using its email method.

Compare:

if user && user.profile && user.profile.avatar

with:

user&.profile&.avatar

The second says:

Traverse this optional object graph.

Compare:

message = <<~TEXT
...
TEXT

with manually concatenating strings.

The first says:

This is a multiline piece of text.

And:

case response
in { status: 200, body: String =>> body }

says:

I expect this particular structure.

That is the real power behind Ruby’s “mysterious symbols.”


33. Senior Ruby Developer Rule: Don’t Optimize for Cleverness

A senior Ruby developer should know all of these constructs.

But knowing them doesn’t mean using them everywhere.

For example:

users.map { _1.orders.select(&:paid?).sum(&:total) }

is valid Ruby.

But:

users.map do |user|
  user.orders
      .select(&:paid?)
      .sum(&:total)
end

may be more readable.

And sometimes the best version is:

users.map do |user|
  paid_orders = user.orders.select(&:paid?)
  paid_orders.sum(&:total)
end

Ruby gives you enormous freedom.

Good Ruby isn’t the shortest Ruby.

Good Ruby is code where another experienced developer can understand the intent quickly.


Final Takeaway

Ruby 3.4 contains a rich collection of compact syntax:

<<~TEXT       # squiggly heredoc
%i[...]       # symbol array
%w[...]       # string array
&.            # safe navigation
&:method      # symbol-to-proc
_1            # numbered parameter
*args         # positional splat
**kwargs      # keyword splat
...           # argument forwarding
1...10        # exclusive range
x ||= value   # conditional assignment
def foo = ... # endless method
case x; in... # pattern matching
=>            # hash rocket / pattern binding / rightward assignment

These aren’t merely Ruby “shortcuts.”

They represent Ruby’s broader design philosophy:

Make the code express what the programmer means, while keeping the syntax close to natural language.

For a senior Ruby/Rails developer, the goal isn’t to remember every symbol.

The goal is to recognize when a piece of Ruby syntax improves the expression of intent – and when it merely makes the code clever.

That distinction is what separates knowing Ruby syntax from writing idiomatic, maintainable Ruby.

Happy Rubying!~

Integrate AI with Rails: AI bootcamp for Developers – Day 5 –Use OpenRouter API, Create AI Chat service

We had a problem making a LLM request to get the response due to the lack of remaining credits in the last part. Let’s solve it in this part using OpenRouter APIs. You can read more about this here: Openrouter ai- one api for multiple ai models

Let’s switch now to OpenRouter’s free-model tier rather than DeepSeek directly. As of April 2026, OpenRouter offers free models at $0 input/output pricing and its openrouter/free router automatically selects an available free model; the free plan currently has a 50-requests/day limit. (OpenRouter)

This is actually a useful improvement for our bootcamp because OpenRouter exposes an OpenAI-compatible API, so we can keep the openai Ruby SDK and change only the endpoint + API key + model. (OpenRouter)

Step 5.17 – Switch Ai::Client to OpenRouter Free

We are not changing our Rails architecture:

Rails
Ai::Client
OpenAI-compatible SDK
OpenRouter
Free LLM

1. Create an OpenRouter API key

Create an account at OpenRouter and create an API key.

It should look approximately like:

sk-or-v1-...

OpenRouter documents this flow in its free-model quickstart. (OpenRouter)

Do not paste the key here.

2. Change Rails credentials

We currently have:

openai:
api_key: ...

Let’s change this to:

openrouter:
api_key: OUR_OPENROUTER_KEY

Run:

bin/rails credentials:edit

Change:

openai:
api_key: ...

to:

openrouter:
api_key: ...

Save and exit.

3. Update Ai::Client

Open:

app/services/ai/client.rb

For now, use:

class Ai::Client
  MODEL = "openrouter/free"
  BASE_URL = "https://openrouter.ai/api/v1"

  def initialize
    @api_key = Rails.application.credentials.dig(:openrouter, :api_key)

    raise "OpenRouter API key is missing" if @api_key.blank?

    @client = OpenAI::Client.new(
      api_key: @api_key,
      base_url: BASE_URL
    )
  end

  def chat(message:)
    @client.chat.completions.create(
      model: MODEL,
      messages: [
        {
          role: "user",
          content: message
        }
      ]
    )
  end
end

OpenRouter explicitly documents using an OpenAI-compatible client by changing the base URL to:

https://openrouter.ai/api/v1

and then using the OpenAI-style chat completions API. (OpenRouter)

Important change

Previously we were using:

@client.responses.create(...)

Now we’re using:

@client.chat.completions.create(...)

That’s intentional. OpenRouter supports Responses API for its free router, but its OpenAI-compatible chat-completions interface is the simplest and most broadly compatible path for this exercise.

4. Test credentials first

Run:

bin/rails c

Then:

Rails.application.credentials.dig(:openrouter, :api_key)

Make sure it returns a value.

Don’t paste it here.

Then:

exit

5. Make the first free LLM request

Run:

bin/rails c

Then:

client = Ai::Client.new

And:

response = client.chat(
message: "Explain Ruby blocks in simple terms."
)

Now inspect:

response

Then:

response.choices.first.message.content

You should get the model’s response.

6. Inspect usage

Run:

response.usage

Then:

response.usage.prompt_tokens

and:

response.usage.completion_tokens

The exact response shape depends on the model/provider, so we’re intentionally inspecting it rather than assuming the field names.

OpenRouter Dashboard – token usage

7. What did we just accomplish?

Our application has now become provider-independent at the architecture level:

                    Ai::Client
                        │
                 ┌──────┴──────┐
                 │             │
              Provider       Provider
                 │             │
              OpenAI       OpenRouter
                                │
                           Free Models

And later we can support:

OpenRouter
  ├── gpt-oss-20b
  ├── Nemotron
  ├── other free models
  └── paid models


OpenRouter currently lists multiple free models, including OpenAI’s gpt-oss-20b and NVIDIA Nemotron variants. (OpenRouter)

We won’t hard-code a specific free model yet because the free-model pool changes over time. openrouter/free is specifically designed to route requests to an available free model.

8. One important lesson

This change demonstrates a valuable architectural idea:

The LLM provider should be an implementation detail behind our AI service boundary.

Today:

Ai::Client → OpenRouter

Later:

Ai::Client → OpenAI

or:

Ai::Client → Anthropic

without changing:

Conversation
Message
ChatService
Controllers
UI

That’s exactly why we created Ai::Client before integrating the provider.


Stop here

Do these steps in order:

bin/rails credentials:edit

Set:

openrouter:
api_key: OUR_OPENROUTER_KEY

Then update Ai::Client as shown above and run:

bin/rails c
client = Ai::Client.new
response = client.chat(
message: "Explain Ruby blocks in simple terms."
)

Then:

response.choices.first.message.content

Once that works, check the output:

➜  ai_assistant git:(main) ✗ rails c
Loading development environment (Rails 8.1.3.1)
ai-assistant(dev):001> client = Ai::Client.new
=> 
#<Ai::Client:0x000000012d5ca138
...
ai-assistant(dev):002* response = client.chat(
ai-assistant(dev):003*   message: "How can I become an expert in Ruby language"
ai-assistant(dev):004> )
=> 
#<OpenAI::Models::Chat::ChatCompletion:0x22c8 {id: "gen-1786952686-y1YoZ2KFkNMw6Le1xdp5", choices: [{finish_reason: :stop, index: 0, logpr...
ai-assistant(dev):005> response.choices.first.message.content
ai-assistant(dev):006> 
=> "User Safety: safe" # our api not started working
ai-assistant(dev):002> conversation = Conversation.first
ai-assistant(dev):003* conversation.messages.order(:created_at).each do |message|
ai-assistant(dev):004*   puts "#{message.role}: #{message.content}"
ai-assistant(dev):005> end
  Message Load (9.9ms)  SELECT "messages".* FROM "messages" WHERE "messages"."conversation_id" = 1 ORDER BY "messages"."created_at" ASC /*application='AiAssistant'*/
user: What is Ruby? # our api not started working
user: What is Ruby? # our api not started working
user: What is Ruby? in 20 words
assistant: Ruby is a dynamic, object‑oriented language emphasizing developer happiness, known for elegant syntax and powerful, full‑featured, open‑source web framework Rails.

OpenRouter free model works!

Then we’ll immediately proceed to the next step: cleanly extracting the provider response and mapping it into our Message model, which is where the application starts becoming a real AI chat application rather than just an API experiment.


Create AI Chat Service, Store Messages

Now make the LLM response usable by Rails, persist it as a Message and introduce Ai::ChatService.

This is the point where our app changes from:

Rails → LLM API

to:

Rails
ChatService
Ai::Client
LLM
ChatService
Message
PostgreSQL

OpenRouter’s OpenAI-compatible API returns the normal chat-completions shape with choices[0].message.content, and the OpenAI Ruby SDK exposes typed response objects with hash-style access as well. (OpenRouter)

Step 6 – Clean up Ai::Client

We don’t want the rest of the application knowing about:

response.choices.first.message.content

That’s provider/SDK-specific knowledge.

Change app/services/ai/client.rb to:

class Ai::Client
  MODEL = "openrouter/free"
  BASE_URL = "https://openrouter.ai/api/v1"

  def initialize
    api_key = Rails.application.credentials.dig(:openrouter, :api_key)

    raise "OpenRouter API key is missing" if api_key.blank?

    @client = OpenAI::Client.new(
      api_key: api_key,
      base_url: BASE_URL
    )
  end

  def chat(message:)
    response = @client.chat.completions.create(
      model: MODEL,
      messages: [
        {
          role: "user",
          content: message
        }
      ]
    )

    {
      content: response.choices.first.message.content,
      model: response.model,
      input_tokens: response.usage.prompt_tokens,
      output_tokens: response.usage.completion_tokens
    }
  end
end

Now Ai::Client has a clean contract:

{
content: "...",
model: "...",
input_tokens: 123,
output_tokens: 456
}

The rest of Rails doesn’t care whether the provider uses choices, output_text, or something else.

Why this abstraction matters

Today:

Ai::Client → OpenRouter

Tomorrow:

Ai::Client → OpenAI

The rest of your application doesn’t change.


Step 7 – Test the new client

Run:

bin/rails c

Then:

client = Ai::Client.new

Then:

result = client.chat(message: "Explain Ruby blocks in two sentences.")

Inspect:

result

You should get something like:

{
content: "...",
model: "...",
input_tokens: 20,
output_tokens: 40
}

This is our internal application-level response.


Step 8 – Create Ai::ChatService

Now create:

app/services/ai/chat_service.rb

Code:

class Ai::ChatService
  def initialize(ai_client: Ai::Client.new)
    @ai_client = ai_client
  end

  def call(conversation:, user_message:)
    user_message_record = conversation.messages.create!(
      role: :user,
      content: user_message
    )

    result = @ai_client.chat(message: user_message)

    assistant_message = conversation.messages.create!(
      role: :assistant,
      content: result[:content],
      model: result[:model],
      input_tokens: result[:input_tokens],
      output_tokens: result[:output_tokens]
    )

    {
      user_message: user_message_record,
      assistant_message: assistant_message
    }
  end
end

This class is now responsible for the application workflow.

Notice the separation:

Ai::Client

How do I talk to the LLM provider?

Ai::ChatService

What should happen when a user sends a chat message?

That’s a very important Rails design boundary.


Step 9 – Test the full flow

Start console:

bin/rails c

Find your conversation:

conversation = Conversation.first

Then:

service = Ai::ChatService.new

Now:

result = service.call(
conversation: conversation,
user_message: "What is Ruby?"
)

Inspect:

result[:user_message]

and:

result[:assistant_message]

Now:

conversation.messages.order(:created_at).each do |message|
puts "#{message.role}: #{message.content}"
end

You should now have:

user: What is Ruby?
assistant: Ruby is ...

Now we have a real persistent AI conversation.


Step 10 – Inspect PostgreSQL

Exit console:

exit

Then:

bin/rails dbconsole

Run:

SELECT
  id,
  conversation_id,
  role,
  model,
  input_tokens,
  output_tokens,
  content
FROM messages
ORDER BY id;

This is important because you’re seeing the complete lifecycle:

User input
Rails
LLM
AI response
Message record
PostgreSQL

Step 11 – Add a transaction

There’s a subtle production problem in our current service.

Imagine:

Save user message ✅
Call AI ✅
Save assistant message ❌

Now the conversation is incomplete.

At minimum, make the persistence workflow transactional:

class Ai::ChatService
  def initialize(ai_client: Ai::Client.new)
    @ai_client = ai_client
  end

  def call(conversation:, user_message:)
    conversation.transaction do
      user_message_record = conversation.messages.create!(
        role: :user,
        content: user_message
      )

      result = @ai_client.chat(message: user_message)

      assistant_message = conversation.messages.create!(
        role: :assistant,
        content: result[:content],
        model: result[:model],
        input_tokens: result[:input_tokens],
        output_tokens: result[:output_tokens]
      )

      {
        user_message: user_message_record,
        assistant_message: assistant_message
      }
    end
  end
end

Important nuance

The database transaction does not roll back an external LLM API call.

That’s a classic distributed-system issue:

PostgreSQL transaction
+
External API

The DB transaction protects your local writes, but it can’t undo the provider request.

Step 12 – Write the first test

Since you have a real service now, let’s test it.

Create:

test/services/ai/chat_service_test.rb

because Rails 8 defaults to Minitest.

Example:

require "test_helper"

class Ai::ChatServiceTest < ActiveSupport::TestCase
  test "persists user and assistant messages" do
    conversation = Conversation.create!(title: "Test")

    fake_client = Minitest::Mock.new

    fake_client.expect(
      :chat,
      {
        content: "Ruby is a programming language.",
        model: "test-model",
        input_tokens: 10,
        output_tokens: 8
      },
      [{ message: "What is Ruby?" }]
    )

    service = Ai::ChatService.new(ai_client: fake_client)

    service.call(
      conversation: conversation,
      user_message: "What is Ruby?"
    )

    assert_equal 2, conversation.messages.count
    assert conversation.messages.user.exists?
    assert conversation.messages.assistant.exists?

    fake_client.verify
  end
end

Run:

bin/rails test test/services/ai/chat_service_test.rb

The important idea is:

The test doesn’t call OpenRouter.

We replace the external dependency with a fake.

That’s exactly how we should test AI integrations.

Update the test

require "test_helper"

class Ai::ChatServiceTest < ActiveSupport::TestCase
  test "persists user and assistant messages" do
    conversation = Conversation.create!(title: "Test")

    fake_client = Minitest::Mock.new

    fake_client.expect(
      :chat,
      {
        content: "Ruby is a programming language.",
        model: "test-model",
        input_tokens: 10,
        output_tokens: 8
      },
      message: "What is Ruby?"
    )

    service = Ai::ChatService.new(ai_client: fake_client)

    service.call(
      conversation: conversation,
      user_message: "What is Ruby?"
    )

    assert_equal 2, conversation.messages.count

    user_message = conversation.messages.user.first
    assistant_message = conversation.messages.assistant.first

    assert_equal "What is Ruby?", user_message.content
    assert_equal "Ruby is a programming language.", assistant_message.content
    assert_equal "test-model", assistant_message.model
    assert_equal 10, assistant_message.input_tokens
    assert_equal 8, assistant_message.output_tokens

    fake_client.verify
  end
end

What We Have Now

We have crossed a significant milestone:

                ┌──────────────────┐
                │   Conversation   │
                └────────┬─────────┘
                         │
                         ▼
                ┌──────────────────┐
                │  ChatService     │
                └────────┬─────────┘
                         │
                         ▼
                ┌──────────────────┐
                │    Ai::Client    │
                └────────┬─────────┘
                         │
                         ▼
                ┌──────────────────┐
                │    OpenRouter    │
                │   Free LLM       │
                └────────┬─────────┘
                         │
                         ▼
                ┌──────────────────┐
                │ Assistant Msg    │
                └────────┬─────────┘
                         │
                         ▼
                    PostgreSQL

This gives you several int. concepts already:

LLM integration, service objects, provider abstraction, persistence, token tracking, transactions, external API boundaries, and testing.

Check our code in this repo: https://github.com/abhilashak/ai_assistant


Next: Step 7 – Conversation Memory + Prompt Builder

Right now, every request is independent.

We’ll change:

"What is Ruby?"

into:

System Prompt
+
Previous Messages
+
Current User Message
LLM

Then we’ll build Ai::PromptBuilder, add conversation history, and after that move quickly into the Chat UI + streaming.

to be continued …

Integrate AI with Rails: AI bootcamp for Developers – Day 4 -Practical Course – Part 3

Great. Now we can make the first real LLM request.

We’ll keep this step deliberately small. Our goal is not to build the complete AI assistant yet.

The goal is simply:

Rails
Ai::Client
OpenAI API
LLM
Response

Once this works, we’ll build the Rails service layer around it.

Step 5.11 – Add the OpenAI Ruby SDK

Rather than manually constructing HTTP requests, we’ll start with the official Ruby SDK.

1. Add the gem

Open our Gemfile and add:

gem "openai"

Then run:

bundle install

Verify:

bundle info ruby-openai

You should see where Bundler installed the gem.

Why use an SDK?

We could use Ruby’s Net::HTTP ourselves:

Ruby
Net::HTTP
HTTP request
OpenAI

But then we’d have to manually handle:

  • authentication headers
  • JSON encoding
  • HTTP errors
  • response parsing
  • request formatting

The SDK gives us:

Ruby
OpenAI Ruby SDK
HTTP
OpenAI

Important point: An SDK doesn’t eliminate the HTTP API. It is an abstraction over it.

Step 5.12 – Verify the gem

Run:

bin/rails console

Then:

require "openai"

It should return:

=> true

or possibly:

=> "openai"

depending on the gem’s load behavior.

Then:

OpenAI

should resolve without a NameError.

Exit:

exit

Step 5.13 – Let’s inspect the SDK before using it

This is something I want you to develop as a senior Ruby developer habit.

Instead of blindly copying code from a blog, let’s see what API the installed gem exposes.

Run:

bundle info ruby-openai

Then:

bin/rails console

Inside console:

require "openai"

Then:

OpenAI::Client.instance_method(:initialize).parameters

This tells us what the client’s constructor expects.

Also try:

OpenAI::Client.instance_methods(false)

We’re learning to inspect a Ruby library rather than treating it as magic.

Step 5.14 – Create the OpenAI client

Now let’s modify:

app/services/ai/client.rb

We’ll start with:

class Ai::Client
  def initialize
    @api_key = Rails.application.credentials.dig(:openai, :api_key)

    raise "OpenAI API key is missing" if @api_key.blank?

    @client = OpenAI::Client.new(api_key: @api_key)
  end
end

Now we have:

Ai::Client
   │
   ├── reads Rails credentials
   │
   └── creates OpenAI SDK client

Step 5.15 – Test initialization

Run:

bin/rails console

Then:

client = Ai::Client.new

It should return something similar to:

#<Ai::Client:0x...>

No request has happened yet.

That’s important.

We’ve only done:

Rails credentials
API key
OpenAI::Client

Stop here

Don’t call the LLM yet.

I want you to complete these steps first:

1. Gemfile

gem "openai"

2. Install

bundle install

3. Verify

bundle info ruby-openai

4. Update

app/services/ai/client.rb

with the code above.

5. Test

bin/rails c
client = Ai::Client.new

One note

The Ruby OpenAI SDK’s API can change between versions, so don’t blindly copy the exact request syntax from older tutorials. That’s why we’re checking the version we’ve actually installed before writing the API call.

Now we’ve:

“OpenAI client initialized.”

We’ll make our first actual LLM request and inspect the complete response, including:

response
model
output
usage
input tokens
output tokens

That will lead directly into why we added those fields to our Message model.


@client = OpenAI::Client.new(api_key: @api_key)

We’ll use our installed SDK’s API, not older ruby-openai examples. The current official openai Ruby SDK documents OpenAI::Client.new(api_key: ...) and the Responses API as the current interface. (GitHub)

Step 5.16 – Make the First Real LLM Request

For this step, we’ll do one simple request and inspect the response.

We are not integrating it with Conversation or Message yet.

Our goal is:

Rails console
Ai::Client
OpenAI Responses API
LLM
Response

1. Add a chat method

Open:

app/services/ai/client.rb

Change it to:

class Ai::Client
  def initialize
    @api_key = Rails.application.credentials.dig(:openai, :api_key)

    raise "OpenAI API key is missing" if @api_key.blank?

    @client = OpenAI::Client.new(api_key: @api_key)
  end

  def chat(message)
    @client.responses.create(
      model: "gpt-5.2",
      input: message
    )
  end
end

The SDK’s current Responses API accepts model and input for creating a response. (GitHub)

Why input: message?

We’re deliberately starting with the simplest possible request:

input: "Explain Ruby blocks in simple terms"

Later we’ll send structured conversation history:

input: [
{ role: :system, content: "..." },
{ role: :user, content: "..." }
]

The Responses API supports both simple input and structured message input. (GitHub)

2. Start Rails console

bin/rails c

Create the client:

client = Ai::Client.new

Now make the request:

response = client.chat(
message: "Explain Ruby blocks in simple terms."
)

This is the moment our application makes an actual network request.

3. Inspect the response

First:

response.class

Then:

response

Don’t worry if the output is large.

The current official Ruby SDK returns typed response objects and the response contains the generated output plus metadata such as usage. (GitHub)

But if you get the following output, we can change the model which has free API calls:

ai-assistant(dev):013> client = Ai::Client.new
ai-assistant(dev):003> res = ai.chat('I want to be a expert in Ruby language')
app/services/ai/client.rb:11:in 'Ai::Client#chat': {url: "https://api.openai.com/v1/responses", status: 429, body: {error: {message: "You have no credits remaining. Add credits to continue using the API at https://platform.openai.com/settings/organization/billing/.", type: "insufficient_quota", param: nil, code: "credit_balance_exhausted"}}} (OpenAI::Errors::RateLimitError)
        from (ai-assistant):3:in '<compiled>'

Yes – the error makes sense and there is an important distinction here:

Our ChatGPT subscription and OpenAI API billing are separate.

So even if you can use ChatGPT normally, that does not give your Ruby application free API calls. OpenAI explicitly says ChatGPT and API billing are managed separately. (OpenAI Help Center)

Why you’re seeing You have no credits remaining

Your Rails code is calling the OpenAI API, not ChatGPT:

Rails app
OpenAI API
API billing / credits

The API account associated with your key currently has no usable credits. OpenAI’s current prepaid-billing documentation says API requests stop once the available credit balance is exhausted. (OpenAI Help Center)

“But aren’t basic models free?”

Not generally for the API.

There may be specific free/trial allocations or products with included usage, but you should not assume that a model being available in ChatGPT means the API is free.

For our Rails application, we’re using:

OpenAI::Client

which consumes API usage and is metered separately.


What I recommend for our course

I don’t think we should spend money just to continue learning unless you’re comfortable doing so.

We have three practical paths:

Option 1 – Add a small API balance

Open your OpenAI API billing overview and check your balance. New API users currently use prepaid billing and the documented minimum purchase is $5, with $10 as the default purchase amount. (OpenAI Help Center)

For this course a small balance should be plenty for experimentation because our prompts will be tiny.

Option 2 – Use another provider with a free tier

We could temporarily use a provider that offers some free API usage, while keeping the same architecture:

Ai::Client
Provider
LLM

This is actually useful because later we’ll make our architecture provider-agnostic.

Option 3 – Run a local model

We can install something like Ollama and run an LLM locally:

Rails
Ai::Client
localhost
Local LLM

Advantages:

  • no API credits
  • no network dependency
  • no per-token cost
  • great for development

The downside is that the model quality may differ from hosted models, and local inference requires reasonable hardware.


One important thing for our architecture

Don’t change this:

Ai::Client

The fact that OpenAI isn’t currently usable doesn’t mean we should redesign the application.

We specifically created:

Rails
Ai::Client
Provider

so that later we can switch:

Ai::Client
OpenAI

to:

Ai::Client
Anthropic

or:

Ai::Client
Ollama

without rewriting our Rails application.

That’s actually an important senior-level design lesson.


What we can do now?

Since our objective is learning AI engineering, not spending money on API calls, first check your API billing page.

If it shows:

Free trial credit remaining: $0.00

then the error is fully explained. OpenAI’s billing documentation uses exactly this sort of balance indicator. (OpenAI Help Center)

We can then decide between a small API credit or a local/free-tier provider.

For this course, I slightly prefer keeping OpenAI as the first provider so you learn the real production API flow, then later we’ll add a second provider/local model to demonstrate the abstraction properly.

4. Get the generated text

Try:

response.output_text

You should get a normal answer such as:

A Ruby block is a chunk of code that can be passed to a method...

This is the first important distinction:

response
entire API response
response.output_text
just the model's text

Don’t immediately throw away the full response. We need the metadata later.

5. Inspect the model

Try:

response.model

This tells you which model actually generated the response.

That’s relevant to our messages.model column.

6. Inspect usage

Now:

response.usage

You should see token-related information.

Inspect it:

response.usage.input_tokens

and:

response.usage.output_tokens

These are directly related to the fields we added earlier:

messages
-------------------
input_tokens
output_tokens

So our database design is now connected to a real API response.

LLM response
├── model
├── output text
└── usage
├── input_tokens
└── output_tokens

The SDK’s response models expose usage information as part of the response. (GitHub)

7. One very important experiment

Ask a second question:

response2 = client.chat(
message: "What is my name?"
)

You’ll probably notice the model doesn’t know your name from the previous request.

That’s intentional.

We made two independent requests:

Request 1
"Explain Ruby blocks"
Request 2
"What is my name?"

The LLM does not automatically receive our previous request.

This is going to become extremely important when we implement:

Conversation
Messages
Prompt Builder
LLM

Our Rails application will be responsible for providing the appropriate conversation context.

8. One thing to notice

We’ve built:

app/services/ai/client.rb

and now:

Ai::Client.new.chat(...)

works.

That’s already a valuable architectural boundary:

Rails application
Ai::Client
OpenAI SDK
OpenAI API

Our controllers won’t need to know:

  • how authentication works,
  • how the SDK works,
  • which API endpoint is used,
  • how OpenAI responses are represented.

That’s why we created the abstraction.

Check our code in this repo: https://github.com/abhilashak/ai_assistant


Stop Here

Run these commands one by one:

bin/rails c
client = Ai::Client.new
response = client.chat(
message: "Explain Ruby blocks in simple terms."
)

Then inspect:

response.output_text
response.model
response.usage
response.usage.input_tokens
response.usage.output_tokens

Don’t paste our API key or any sensitive output anywhere.

Now: “Our First LLM request works.”

Then we’ll do the next important step: inspect the raw response structure and improve Ai::Client so it returns a clean Ruby object to the rest of our Rails application.


Integrate AI with Rails: AI bootcamp for Developers – Day 4 -Practical Course – Part 2

Now let’s move to the next step: make the Message model production-friendly.

We’ll start with the most important field: role.

Step 3 – Design Message.role

Currently our database allows:

role = anything

For example:

"user"
"assistant"
"system"
"foo"
"hello"
"something-invalid"

That’s not what we want.

Our AI application has a defined set of roles:

user
assistant
system

Later, when we introduce tool calling, we may also need to represent tool messages depending on the provider/API design. But for our current application, we’ll keep the persisted roles to these three.

Why use a string instead of an integer?

You may remember our previous discussion about Rails enums.

We could store:

0 = user
1 = assistant
2 = system

But for an AI application, I prefer a string-backed enum.

Database:

role
---------
user
assistant
system

instead of:

role
---------
0
1
2

Why?

1. Database is self-describing

When you run:

SELECT role FROM messages;

you immediately see:

user
assistant
assistant
user
system

2. Easier debugging

When you’re debugging an AI conversation, the actual value is obvious.

3. Safer for external APIs

LLM APIs already use strings such as:

{
"role": "user"
}

So our database representation matches the domain.

Step 3A – Add the Rails enum

Open:

app/models/message.rb

Currently you should have something like:

class Message < ApplicationRecord
belongs_to :conversation
end

Change it to:

class Message < ApplicationRecord
belongs_to :conversation
enum :role, {
user: "user",
assistant: "assistant",
system: "system"
}, validate: true
end

Understand this carefully

This:

enum :role, {
user: "user",
assistant: "assistant",
system: "system"
}, validate: true

doesn’t mean PostgreSQL has an enum type. We’re using a Rails enum backed by a string column.

PostgreSQL still has:

role character varying

Rails gives us a domain API on top of it.

Step 3B – Test the enum

Start Rails console:

bin/rails console

Find our message:

message = Message.first

Check:

message.role

You should get:

"user"

Now:

message.user?

Expected:

true

And:

message.assistant?

Expected:

false

Step 3C – Test the scopes

Rails also gives us useful scopes.

Try:

Message.user

and:

Message.assistant

and:

Message.system

For example:

Message.user

roughly translates to:

SELECT *
FROM messages
WHERE role = 'user';

This is one of the benefits of using an enum.

Step 3D – Test invalid values

Now try:

Message.new(
conversation: Conversation.first,
role: "something_else",
content: "test"
)

Because we specified:

validate: true

Rails should treat the role as invalid.

Check:

message = Message.new(
conversation: Conversation.first,
role: "something_else",
content: "test"
)
message.valid?

Expected:

false

Then:

message.errors.full_messages

You should see an error indicating that the role is not included in the allowed values.

Why validate: true?

This is worth understanding: Without validation, Rails enum behavior can raise an ArgumentError when assigning an invalid value.

With:

validate: true

we get normal ActiveRecord validation behavior:

message.valid?
false

and:

message.errors

contains the validation error.

That’s generally more convenient when the model is receiving user/application input.

Step 3E – One more important layer: Database constraint

There is a subtle issue here.

Rails validation protects you when data enters through Rails.

But PostgreSQL doesn’t know that only these values are valid:

user
assistant
system

Someone could execute:

INSERT INTO messages (conversation_id, role, content)
VALUES (1, 'invalid', '...');

directly against PostgreSQL.

The database would currently allow it.

This leads to an important senior-engineering principle:

Application-level validation and database-level integrity are complementary.

We’ll add a database constraint.

But don’t do that yet. First make sure the Rails enum works.

After that, we’re finally ready for the exciting part:

Rails
Ai::Client
LLM API
Real AI response

Now let’s strengthen the model at the database level.

You currently have Rails validation:

enum :role, {
user: "user",
assistant: "assistant",
system: "system"
}, validate: true

That’s good, but a senior Rails application shouldn’t rely only on model validation for important data integrity.

Step 4 – Add Database Constraints

We want PostgreSQL itself to enforce:

role MUST be:
user
assistant
system

and:

content MUST NOT be NULL
role MUST NOT be NULL

This gives us two layers:

Rails
Model validation
PostgreSQL
Database constraint

4.1 Why NULL matters

Currently this is possible at the database level:

role = NULL

But an AI message without a role doesn’t make sense.

Likewise:

content = NULL

doesn’t represent a meaningful message.

So we’ll make both required.

4.2 Create a new migration

Don’t modify the old migration because it has already been executed and committed.

Generate a new migration:

bin/rails generate migration AddMessageConstraints

Rails should create:

db/migrate/XXXXXXXXXXXXXX_add_message_constraints.rb

Open that file.

4.3 Add NOT NULL constraints

Put this inside change:

class AddMessageConstraints < ActiveRecord::Migration[8.1]
def change
change_column_null :messages, :role, false
change_column_null :messages, :content, false
end
end

So conceptually:

def change
change_column_null :messages, :role, false
change_column_null :messages, :content, false
end

4.4 Add PostgreSQL CHECK constraint

Now we want PostgreSQL to enforce:

role IN ('user', 'assistant', 'system')

Add:

add_check_constraint(
:messages,
"role IN ('user', 'assistant', 'system')",
name: "messages_role_check"
)

Our migration becomes:

class AddMessageConstraints < ActiveRecord::Migration[8.1]
def change
change_column_null :messages, :role, false
change_column_null :messages, :content, false
add_check_constraint(
:messages,
"role IN ('user', 'assistant', 'system')",
name: "messages_role_check"
)
end
end

4.5 Run the migration

Execute:

bin/rails db:migrate

You should see Rails successfully applying the migration.

4.6 Inspect PostgreSQL

This is worth doing because understand what’s actually happening underneath Rails.

Run:

bin/rails dbconsole

Then:

\d messages

Look toward the bottom.

You should see a check constraint similar to:

messages_role_check
CHECK ((role)::text = ANY (...))

The exact display can vary by PostgreSQL version.

Also check:

\d+ messages

4.7 Test the database constraint

Now let’s prove that PostgreSQL protects us even if Rails is bypassed.

Inside psql, try:

INSERT INTO messages
(conversation_id, role, content, created_at, updated_at)
VALUES
(1, 'invalid', 'This should fail', NOW(), NOW());

You should get an error similar to:

ERROR: new row for relation "messages" violates check constraint "messages_role_check"

That’s exactly what we want.

The database is now protecting the data.

Why is this important?

Suppose an int. asks:

“Why do you have both Rails validation and a PostgreSQL constraint?”

A strong senior-level answer would be:

“Rails validations provide application-level feedback and are useful for normal model operations, but they’re not a database integrity guarantee because data can enter through other paths. For important invariants such as message roles, I also enforce the constraint at the PostgreSQL level.”

That’s a much stronger answer than:

“Because Rails has validations.”

4.8 One more design question: content

We’re making:

change_column_null :messages, :content, false

But should an AI message be allowed to contain an empty string?

For example:

content: ""

NOT NULL allows that.

So:

NULL NO
"" technically allowed
"Hello" YES

Whether empty content should be allowed is an application-level business rule.

We can later decide whether to add:

validates :content, presence: true

But don’t add that yet.

There are legitimate AI API situations where a message may not have ordinary text content – for example, tool-related or structured content. We’ll revisit our message representation when we implement tool calling.

4.9 Test a valid message

Exit psql:

\q

Then:

bin/rails c

Run:

conversation = Conversation.first

Then:

message = conversation.messages.create(
role: :user,
content: "What is Ruby?"
)

Check:

message.persisted?

You should get:

true

And:

message.role

should return:

"user"

Stop Here

Please do these in order:

bin/rails generate migration AddMessageConstraints

Edit the migration with the constraints above.

Then:

bin/rails db:migrate

Verify with:

bin/rails dbconsole
\d messages

Then test the invalid role directly in PostgreSQL.

Finally:

git add app/models/message.rb db/migrate
git commit -m "feat: validate message roles"
git push

NOW: “Message constraints are done.”

Then we move to the big milestone: Our First Real LLM API Call


Excellent. We now have a clean foundation:

Ruby 3.4.1
Rails 8.1
PostgreSQL
Conversation
└── Message
├── role
├── content
├── model
├── input_tokens
└── output_tokens
Ai::Client

Now we reach the first real AI step.

Step 5 – Make Our First LLM API Call

We’re going to do this in a deliberately controlled way.

Don’t build the Chat UI yet.

First, we need to understand:

Ruby
Ai::Client
HTTP request
LLM provider
HTTP response
Ruby

Once we understand this, we’ll wrap it nicely into Rails architecture.

5.1 First decision – which provider?

For this practical course, let’s start with OpenAI.

Not because you must use OpenAI in production, but because it gives us a straightforward API to understand the fundamentals.

Later we’ll discuss:

Rails
├── OpenAI
├── Anthropic
└── Gemini

and how to design our Ai::Client so that we’re not tightly coupled to one provider.

5.2 Before writing code – understand the request

Conceptually, we’re going to send something like:

POST /v1/responses
{
"model": "...",
"input": "Explain Ruby blocks in simple terms."
}

The provider’s server processes the request:

Rails
│ HTTPS
OpenAI API
LLM
Response

The important thing to understand is:

An LLM API is an HTTP API.

The Ruby SDK is just a convenient abstraction around HTTP.

5.3 Check our Ai::Client

You already created:

app/services/ai/client.rb

Open it.

If it currently contains nothing useful, that’s completely fine.

For now, make it:

# app/services/ai/client.rb

class Ai::Client
end

Don’t add API code yet.

5.4 Configure the API key securely

Do not put our API key in Ruby source code.

We have two common approaches:

Environment variables

or:

Rails encrypted credentials

For this project, I’m going to use Rails encrypted credentials because it’s a good opportunity to understand how Rails handles secrets.

5.5 Create Rails encrypted credentials

Run:

➜  ai_assistant git:(main) ✗ VISUAL="code --wait" rails credentials:edit

Rails will open our configured editor.

Add:

openai:
api_key: OUR_OPENAI_API_KEY

For example:

openai:
api_key: sk-xxxxxxxxxxxxxxxx

Use our actual API key locally, but never paste it into this conversation or commit it to GitHub.

Save and close the editor

What’s actually happening?

Rails creates/uses:

config/credentials.yml.enc

This file is encrypted.

Our encryption key is stored separately in:

config/master.key

The important rule is:

config/credentials.yml.enc
COMMIT
GitHub

is okay.

But:

config/master.key

should never be committed to GitHub.

Check:

git status

You should not see:

config/master.key

as a file to commit.

5.6 Verify Rails can read the key

Run:

bin/rails console

Then:

Rails.application.credentials.dig(:openai, :api_key)

You should get our key back, just verify that it returns a string rather than nil.

Then:

exit

5.7 Why use dig?

Our credentials structure is:

openai:
api_key: ...

which Rails exposes approximately as:

{
openai: {
api_key: "..."
}
}

So:

Rails.application.credentials.dig(:openai, :api_key)

means:

credentials
openai
api_key

This is cleaner than accessing nested values manually.

5.8 Now configure Ai::Client

Open:

app/services/ai/client.rb

Change it to:

class Ai::Client
def initialize
@api_key = Rails.application.credentials.dig(:openai, :api_key)
end
end

Now the client knows how to retrieve its secret.

5.9 Add a safety check

We don’t want the application to fail mysteriously later.

Add:

class Ai::Client
  def initialize
    @api_key = Rails.application.credentials.dig(:openai, :api_key)

    raise "OpenAI API key is missing" if @api_key.blank?
  end
end

Now:

Ai::Client.new

will fail immediately if the key isn’t configured. This is called fail-fast configuration.

5.10 Test the client

Run:

bin/rails console

Then:

client = Ai::Client.new

If everything is configured correctly, it should return:

#<Ai::Client:0x...>

No API request has happened yet.

We’re only testing:

Rails credentials
Ai::Client

Check our code in this repo: https://github.com/abhilashak/ai_assistant


Stop Here

Don’t make the API request yet.

complete only these steps first:

1. Configure credentials

bin/rails credentials:edit

with:

openai:
api_key: OUR_KEY

2. Verify:

bin/rails console
Rails.application.credentials.dig(:openai, :api_key)

Don’t show me the key.

3. Update:

app/services/ai/client.rb

to:

class Ai::Client
def initialize
@api_key = Rails.application.credentials.dig(:openai, :api_key)
raise "OpenAI API key is missing" if @api_key.blank?
end
end

4. Test:

client = Ai::Client.new

Now: “Ai::Client credentials is done.”

Next topic: Step 5.11: install/configure the OpenAI Ruby client and make the first actual LLM request.

to be continued ..

Integrate AI with Rails: AI bootcamp for Developers – Day 4 -Practical Course – Part 1

For a Senior Rails developer, simply knowing what RAG, LLM, Agents, and embeddings mean is not enough. In an int., you may be asked:

“Okay, let’s build an AI feature in Rails. How would you structure it?”

You should be able to open your laptop and actually build one.

So let’s turn Day 4 into a hands-on mini-project in this blog that we’ll build incrementally. We won’t rush through the whole application in one answer.

Build an AI Application with Ruby on Rails

Project: AI Chat Assistant

We’re going to build a real Rails application that evolves throughout this course.

The final architecture will look approximately like this:

                         ┌──────────────────┐
│ Browser │
│ Chat UI │
└────────┬─────────┘


┌──────────────────┐
│ Rails Controller │
└────────┬─────────┘


┌──────────────────┐
│ Chat Service │
└────────┬─────────┘

┌────────────┴────────────┐
▼ ▼
Conversation Prompt Builder
DB │

┌──────────────────┐
│ AI Client │
└────────┬─────────┘


┌──────────────────┐
│ LLM │
│ OpenAI / Claude │
└────────┬─────────┘


Response Formatter


Rails / Browser

And later we’ll evolve it into:

                         AI Rails Application

┌──────────────────────────┼──────────────────────────┐
│ │ │
▼ ▼ ▼
Chat RAG Agents
│ │ │
▼ ▼ ▼
LLM API pgvector Tools
│ │
▼ ▼
Documents Business APIs

That will give you practical experience across LLM → RAG → Agents.


What We Are Going to Build

Our application will start simple.

Version 1

User
Rails
LLM API
Response

Then we’ll progressively add:

Version 2

Conversation
├── User message
├── Assistant response
├── User message
└── Assistant response

Version 3

Streaming:

LLM
token
token
token
Browser

Version 4

Production architecture:

Controller
Chat Service
Prompt Builder
AI Client
Provider

Version 5

RAG:

Question
Embedding
pgvector
Relevant Documents
Prompt
LLM

Version 6

Agent:

User
Agent
├── Search Product
├── Find Order
├── Search Documentation
└── Create Support Ticket

This is why I recommend we build one application throughout the AI bootcamp, rather than writing isolated examples.


Practical Course Roadmap

We’ll divide the practical Day 4 into 10 stages.

StageWhat we’ll buildMain skill
1Rails project setupAI Rails environment
2First LLM requestLLM API
3AI service objectRails architecture
4Chat UIRails frontend
5Conversation persistencePostgreSQL
6Prompt BuilderPrompt architecture
7StreamingReal-time AI UX
8Error handling & retriesProduction engineering
9TestingAI application testing
10Production architectureSenior-level system design

Then Day 5 can build on this application to introduce agents.


Stage 1 – Create the Rails Application

We’ll use:

  • Ruby
  • Rails
  • PostgreSQL
  • OpenAI API initially
  • RSpec/Minitest depending on your preference
  • dotenv/credentials for secrets
  • Turbo/Stimulus where useful

The important thing is:

We won’t use a huge AI framework initially.

I want you to understand what is actually happening underneath.

Later we can compare this approach with Ruby AI libraries/frameworks.

Step 1 – Create Rails App

Assuming Rails is installed:

rails new ai_assistant -d postgresql

Move into the application:

cd ai_assistant

Create database:

bin/rails db:create

Run it:

bin/rails server

Then open:

http://localhost:3000

At this point:

Browser
Rails
PostgreSQL

works.

No AI yet.

Why Start This Way?

This is important for ints.

We don’t want to hide everything behind an AI gem.

You need to understand:

HTTP Request
Rails
Ruby
HTTP Client
AI Provider

Once you understand this, an SDK becomes just an abstraction.


Stage 2 – Configure AI Credentials

Never do this:

api_key = "sk-xxxxx"

Never commit API keys to Git.

We’ll use Rails credentials or environment variables.

Conceptually:

Rails Application
Configuration
OPENAI_API_KEY

For local development, we’ll configure the key securely.


Stage 3- Make Your First LLM Request

This is our first major milestone.

We’ll create:

app/
└── services/
└── ai/
└── client.rb

Initially:

class Ai::Client
def initialize
...
end
def chat(messages:)
...
end
end

Then:

client = Ai::Client.new
response = client.chat(
messages: [
{
role: "user",
content: "Explain Ruby blocks in simple terms"
}
]
)

And eventually:

Ruby
Ai::Client
OpenAI API
LLM
JSON Response
Ruby

This is the most important practical exercise of Day 4.

You will see exactly what an LLM API actually returns.


Stage 4 – Understand the Raw API Response

We’re not immediately going to hide the response.

We’ll inspect things like:

response
├── id
├── model
├── choices
│ └── message
│ ├── role
│ └── content
└── usage
├── input tokens
└── output tokens

This connects directly with Day 1.

Remember:

Tokens
Cost
Latency
Context

You’ll actually see token usage in a real application.


Stage 5 – Build the Rails Chat Application

Now we’ll create:

User
Chat page
POST /conversations/:id/messages
Rails Controller
AI Service
LLM
Response
Browser

We’ll create models such as:

User
Conversation
Message

A conversation:

Conversation
├── Message
│ role: user
│ content: "What is Ruby?"
├── Message
│ role: assistant
│ content: "Ruby is..."
├── Message
│ role: user
│ content: "Who created it?"
└── Message
role: assistant
content: "Yukihiro Matsumoto..."

Stage 6 – Database Design

We’ll design this properly rather than putting everything into one table.

For example:

conversations
-----------------
id
user_id
title
created_at
updated_at

and:

messages
-----------------
id
conversation_id
role
content
model
input_tokens
output_tokens
created_at

Potentially later:

total_tokens
latency_ms
finish_reason

Now you’re thinking like a senior engineer.


Stage 7 – Build Conversation Context

This is where you’ll see something very important.

The LLM doesn’t automatically remember our database conversation.

If we have:

User:
My name is Abhilash.
Assistant:
Nice to meet you.
User:
What's my name?

Rails must send appropriate history back to the LLM:

[
{
role: "user",
content: "My name is Abhi."
},
{
role: "assistant",
content: "Nice to meet you."
},
{
role: "user",
content: "What's my name?"
}
]

Therefore:

Your Rails application manages conversation memory.

This is a very important int. concept.


Stage 8 – Prompt Builder

Eventually we don’t want:

messages = [
...
]

scattered everywhere.

We’ll create:

Ai::PromptBuilder

Architecture:

Conversation
Prompt Builder
System Prompt
+
Conversation History
+
Current User Message
LLM

For example:

Ai::PromptBuilder.new(
conversation: conversation,
user_message: message
).build

This is where your Rails architecture skills become important.


Stage 9 – Streaming

After normal request/response works, we’ll make it feel like ChatGPT.

Instead of:

User
[wait 5 seconds]
↓ Complete response

we’ll have:

User
Rails
LLM
"Ruby"
" is"
" a"
" programming"
" language"

The browser updates progressively.

We’ll investigate Rails approaches such as:

SSE
Turbo Streams
Action Cable

And we’ll discuss when each is appropriate.


Stage 10 – Production Concerns

Then we’ll deliberately break our application.

We’ll simulate:

LLM timeout
LLM rate limit
Invalid response
API unavailable
Malformed JSON

We’ll build:

Ai::Client
├── timeout
├── retry
├── rate limit
└── provider error

We’ll also add:

Authentication
Authorization
Rate limiting
Logging
Token tracking
Cost tracking

This is where my 15 years of backend experience can help.


Stage 11 – Testing

We’ll write tests around:

Ai::Client

Does it call the provider?
Does it handle errors?
Does it parse the response?

Ai::PromptBuilder

Does it create the correct messages?
Does it include conversation history?

Ai::ChatService

Does it save the user message?
Does it call the AI?
Does it save the response?

We’ll mock the external AI service.

The tests should not depend on a live LLM API.


Final Day 4 Application

At the end of the practical course, you’ll have something approximately like:

                         Browser


┌───────────────┐
│ Chat UI │
└───────┬───────┘


┌───────────────┐
│ Controller │
└───────┬───────┘


┌───────────────┐
│ Chat Service │
└───────┬───────┘

┌─────────┴─────────┐
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Conversation │ │Prompt Builder│
│ PostgreSQL │ └──────┬───────┘
└──────────────┘ │

┌──────────────┐
│ AI Client │
└──────┬───────┘


┌──────────────┐
│ LLM │
└──────┬───────┘


Response


Browser

But We Won’t Stop There

This application will become our AI laboratory for the remaining bootcamp.

Day 5

We’ll add:

AI Agent
├── Product Search Tool
├── Order Lookup Tool
└── Documentation Search Tool

Day 6

We’ll add:

RAG
Documents
Chunks
Embeddings
pgvector
Semantic Search
LLM

and discuss production concerns.

Day 7

We’ll turn everything into:

                         AI Rails Application

┌───────────────────┼───────────────────┐
│ │ │
▼ ▼ ▼
LLM RAG Agent
│ │ │
▼ ▼ ▼
Prompting pgvector Tools
│ │ │
└───────────────────┼───────────────────┘

Production System

Then we’ll do AI int. questions + AI system design + coding exercises.


How I Suggest We Learn Each Stage

This is important.

I don’t want to dump 500 lines of Rails code on you.

For every stage, we’ll follow:

1. Understand

I’ll explain:

What are we building?

2. Why

Why do we need this architecture?

3. Build

We’ll create the files/commands one at a time.

4. Run

You’ll execute it on your Mac.

5. Inspect

We’ll look at:

Rails logs
HTTP requests
API responses
PostgreSQL records
LLM responses
Token usage

6. Break it

I’ll give you scenarios such as:

What happens if the AI provider times out?

You solve it.

7. Questions

We’ll turn what you just built into question / answers.

This is particularly important for you because your objective isn’t merely to build an AI app – it’s to be able to explain the architecture during a senior-level discussions.


Our Practical Course

So I suggest we proceed in this exact order:

DAY 4 PRACTICAL
Part 1
├── 1. Create Rails app
├── 2. Configure AI API credentials
├── 3. Make first LLM API request
├── 4. Inspect raw response
├── 5. Build Ai::Client
├── 6. Build Chat UI
├── 7. Conversation + Message models
├── 8. Build Ai::ChatService
├── 9. Build PromptBuilder
├── 10. Conversation memory
├── 11. Streaming
├── 12. Error handling
├── 13. Token/cost tracking
├── 14. Security
├── 15. Tests
└── 16. Production architecture

Start Here: Part 1

Your first assignment is simply to create the application.

On your Mac:

ruby -v
rails -v
psql --version

Then:

rails new ai_assistant -d postgresql
cd ai_assistant
bin/rails db:create
bin/rails server

Verify:

http://localhost:3000

Once that works, don’t start building anything else yet.

Since we’ve already created:

app/services/ai/client.rb

we’ll now build the database layer.

For now, don’t create all models at once. We’ll create one model, migrate it, inspect the database, understand why we designed it this way, and only then move to the next model.


Part 1 – Create Conversation

Our AI application needs to remember conversations.

Think of ChatGPT:

Conversation
├── User message
├── AI response
├── User message
└── AI response

So we’ll have two main models:

Conversation
└── has_many :messages

and later:

Message
└── belongs_to :conversation

For the moment, we’ll create only Conversation.


Step 1 – Check your current directory

From your Rails application’s root:

pwd

You should be somewhere like:

.../ai_assistant

Then:

ls

You should see something similar to:

Gemfile
Gemfile.lock
app
config
db
lib
public
...

If you’re already in your ai_assistant directory, continue.


Step 2 – Generate the Conversation model

Run:

bin/rails generate model Conversation title:string

You can also use:

bin/rails g model Conversation title:string

Both commands do the same thing.

Rails should generate something similar to:

invoke active_record
create db/migrate/XXXXXXXXXXXXXX_create_conversations.rb
create app/models/conversation.rb

Step 3 – Understand what Rails created

Open:

app/models/conversation.rb

You’ll initially see:

class Conversation < ApplicationRecord
end

At this point, the model doesn’t have any associations.

That’s okay.


Step 4 – Inspect the migration

Open:

db/migrate/XXXXXXXXXXXXXX_create_conversations.rb

You’ll see something like:

class CreateConversations < ActiveRecord::Migration[8.1]
def change
create_table :conversations do |t|
t.string :title
t.timestamps
end
end
end

The exact Rails migration version will depend on your Rails version.

What does this mean?

Rails is asking PostgreSQL to create approximately:

conversations
-------------------------
id
title
created_at
updated_at

Step 5 – Run the migration

Now execute:

bin/rails db:migrate

You should see something similar to:

== ... CreateConversations: migrating =====================
-- create_table(:conversations)
-> 0.00xxs
== ... CreateConversations: migrated ======================

Now the table exists in PostgreSQL.


Step 6 – Verify using Rails

Open Rails console:

bin/rails console

or:

bin/rails c

Then:

Conversation

You should get:

Conversation (call 'Conversation.connection' to establish a connection)

Now:

Conversation.column_names

You should see something similar to:

[
"id",
"title",
"created_at",
"updated_at"
]

This is a good habit for you as a senior Rails developer:

Don’t blindly trust generated migrations. Inspect what Rails actually created.


Step 7 – Create a Conversation

Still inside Rails console:

conversation = Conversation.create(title: "My first AI conversation")

You should get something like:

#<Conversation id: 1, title: "My first AI conversation", ...>

Now:

conversation.id

You should get:

1

And:

Conversation.all

should return your conversation.


Step 8 – Check PostgreSQL directly

This is particularly useful for your int. preparation because I want you to understand both Rails and the database underneath it.

Exit Rails console:

exit

Then connect to your database:

bin/rails dbconsole

You’ll enter psql.

Run:

\d conversations

You should see something approximately like:

Column | Type
-------------+--------------------------
id | bigint
title | character varying
created_at | timestamp
updated_at | timestamp

Then:

SELECT * FROM conversations;

You should see your test conversation.

Exit:

\q

Why are we starting with Conversation?

Eventually our application will look like:

Conversation
│ has_many
Messages
├── user
├── assistant
├── user
└── assistant

For example:

Conversation #1
Title: Ruby Question
Message #1
role: user
content: "What is a Ruby block?"
Message #2
role: assistant
content: "A Ruby block is..."
Message #3
role: user
content: "Can you give me an example?"
Message #4
role: assistant
content: "Sure..."

The Conversation represents the container, while Message represents each individual interaction.


One Important Design Decision

You may notice that our earlier architecture discussed:

Conversation
user_id
title

We’re deliberately not adding user_id yet.

Why?

Your newly created Rails app may not have an authentication/User model yet.

We don’t want to introduce Devise/authentication just to learn AI.

We’ll first make the AI application work.

Later we can add:

User
└── has_many :conversations

That keeps today’s exercise focused.


Your Current State

You should now have:

app/
├── models/
│ └── conversation.rb
└── services/
└── ai/
└── client.rb
db/
└── migrate/
└── XXXXX_create_conversations.rb

And PostgreSQL:

conversations
-------------------------
id
title
created_at
updated_at

Stop Here

Don’t create Message yet.

First execute these steps:

bin/rails g model Conversation title:string
bin/rails db:migrate
bin/rails c

Then inside Rails console:

Conversation.column_names

and:

conversation = Conversation.create(title: "My first AI conversation")

Then verify:

Conversation.all

Now Our “Conversation model is done.”

Next step: create the Message model and I’ll explain why role, content, model and token-related columns belong there.

Now I’ll create the Message model. This is the most important model in our AI chat application because it represents the actual conversation between the user and the LLM.

Step 2 – Create the Message model

Our structure will become:

Conversation
├── Message
├── Message
├── Message
└── Message

For example:

Conversation #1
├── User → "What is Ruby?"
├── Assistant → "Ruby is a programming language..."
├── User → "Who created it?"
└── Assistant → "Ruby was created by..."

The Message table needs to know:

  • which conversation it belongs to
  • who/what produced it (user or assistant)
  • the actual message
  • which AI model generated the response
  • token usage, which we’ll use later for cost tracking

Step 1 – Generate the model

From your Rails application’s root directory:

bin/rails generate model Message \
conversation:references \
role:string \
content:text \
model:string \
input_tokens:integer \
output_tokens:integer

You can also write it as one line:

bin/rails g model Message conversation:references role:string content:text model:string input_tokens:integer output_tokens:integer

Rails should generate:

app/models/message.rb
db/migrate/XXXXXXXXXXXXXX_create_messages.rb

Step 2 – Inspect the generated migration

Open:

db/migrate/XXXXXXXXXXXXXX_create_messages.rb

You’ll see something similar to:

class CreateMessages < ActiveRecord::Migration[8.0]
def change
create_table :messages do |t|
t.references :conversation, null: false, foreign_key: true
t.string :role
t.text :content
t.string :model
t.integer :input_tokens
t.integer :output_tokens
t.timestamps
end
end
end

Your Rails migration version may differ.

Understand conversation:references

This is important.

When we wrote:

conversation:references

Rails generated:

t.references :conversation, null: false, foreign_key: true

This creates:

conversation_id

in the messages table.

So our database relationship becomes:

conversations
----------------
id
title
messages
----------------
id
conversation_id
role
content
model
input_tokens
output_tokens
created_at
updated_at

The important connection is:

messages.conversation_id
conversations.id

That’s a standard relational database foreign key.

Step 3 – Run the migration

Execute:

bin/rails db:migrate

You should see something like:

== CreateMessages: migrating ===============================
-- create_table(:messages)
-> ...
== CreateMessages: migrated ================================

Now PostgreSQL has the messages table.

Step 4 – Inspect PostgreSQL

Let’s verify what actually happened.

Run:

bin/rails dbconsole

Then:

\d messages

You should see something approximately like:

Column | Type
----------------+--------------------------
id | bigint
conversation_id | bigint
role | character varying
content | text
model | character varying
input_tokens | integer
output_tokens | integer
created_at | timestamp
updated_at | timestamp

And importantly, you’ll see a foreign key from:

conversation_id

to:

conversations.id

You can also run:

SELECT * FROM messages;

Currently there should be no records.

Exit:

\q

Step 5 – Inspect the generated Rails model

Open:

app/models/message.rb

Rails should have generated:

class Message < ApplicationRecord
belongs_to :conversation
end

Rails automatically added:

belongs_to :conversation

because we used:

conversation:references

Now we need the other side of the relationship.

Step 6 – Add has_many to Conversation

Open:

app/models/conversation.rb

Currently it probably looks like:

class Conversation < ApplicationRecord
end

Change it to:

class Conversation < ApplicationRecord
has_many :messages, dependent: :destroy
end

Now our Rails relationship is:

Conversation
│ has_many
Messages

and:

Message
│ belongs_to
Conversation

Step 7 – Test the association

Open Rails console:

bin/rails console

First find your conversation:

conversation = Conversation.first

Then:

conversation.messages

It should return:

[]

because we haven’t created any messages yet.

Now create a user message:

message = conversation.messages.create(
role: "user",
content: "What is Ruby?"
)

Now:

message

You should get something similar to:

#<Message
id: 1,
conversation_id: 1,
role: "user",
content: "What is Ruby?",
...
>

Step 8 – Check the relationship

Now run:

conversation.messages

You should see your message.

And:

message.conversation

should return the conversation.

This demonstrates the two-way ActiveRecord relationship:

conversation.messages
Message
message.conversation
Conversation

Why do we need role?

This is extremely important for an AI application.

The LLM needs to distinguish between:

user
assistant
system

For example:

{
role: "user",
content: "What is Ruby?"
}

and:

{
role: "assistant",
content: "Ruby is a programming language..."
}

Later, Rails will retrieve these database records and transform them into the messages we send to the LLM.

So:

PostgreSQL
Message
role = "user"
content = "What is Ruby?"
Rails transforms it
LLM API
{
role: "user",
content: "What is Ruby?"
}

This is the bridge between our database and the LLM API.

Why model?

Suppose today we use one model:

some-current-model

Later we change to another model.

We want to know which model generated each response.

For example:

Message #1
model = model-A
Message #2
model = model-B

This becomes valuable for:

  • debugging
  • cost analysis
  • performance analysis
  • comparing models
  • auditing

We don’t need to populate it for user messages.

Why input_tokens and output_tokens?

Remember Day 1?

Input tokens
+
Output tokens
=
Usage

Suppose an AI response used:

input_tokens = 500
output_tokens = 200

We can store that information.

Later we can calculate:

How much did this conversation cost?
How much did this user cost?
Which model is expensive?
Which endpoint consumes the most tokens?

This is exactly the kind of thing you should do in a senior-level AI system design.

One thing we’re deliberately NOT doing yet

You may wonder:

Why don’t we add validations for role?

For example:

validates :role, inclusion: {
in: %w[user assistant system]
}

We’re going to discuss this next.

There is an interesting design question here:

Should role be a Ruby enum?

For example:

enum :role, {
user: "user",
assistant: "assistant",
system: "system"
}

We’ll discuss why a string enum is useful here, what gets stored in PostgreSQL, and what tradeoffs exist before finalising the model.

Our Current Database

After completing this step, you should have:

conversations
-------------------------
id
title
created_at
updated_at
│ 1 → many
messages
-------------------------
id
conversation_id
role
content
model
input_tokens
output_tokens
created_at
updated_at

And Rails:

class Conversation < ApplicationRecord
has_many :messages, dependent: :destroy
end
class Message < ApplicationRecord
belongs_to :conversation
end

Stop Here

Please execute only these steps now:

bin/rails g model Message conversation:references role:string content:text model:string input_tokens:integer output_tokens:integer
bin/rails db:migrate
bin/rails console

Then test:

conversation = Conversation.first
message = conversation.messages.create(
role: "user",
content: "What is Ruby?"
)
conversation.messages
message.conversation

Now Our “Message model done.”

Next step: properly design Message.role and validations, and after that we’ll make our first real LLM API call through your Ai::Client.

to be continued..

Understanding Enums: Why They Exist, How They Work, and How Rails Implements Them

Enums are one of those features developers use frequently – especially in frameworks like Rails – but many developers never fully understand why enums exist, what problem they solve, or how they are implemented internally. In Rails, enums appear deceptively simple:

enum status: { pending: 0, paid: 1, failed: 2 }

But behind this tiny line lies an important software design concept used across programming languages, databases, compilers, APIs, operating systems, and application architecture.

This article explains the complete picture of enums:

  • Why enums exist
  • How they differ from other data structures
  • How Rails maps enums to integers internally
  • Whether enums are tied to SQL/databases
  • How ActiveRecord::Enum works under the hood
  • Real-world benefits and tradeoffs developers should know

What Is an Enum?

An Enum (Enumeration) is a restricted set of named values representing a finite group of states or options.

Example:

status = :pending

Possible statuses may be:

:pending
:processing
:completed
:failed

Instead of allowing any arbitrary value, enums constrain the system to a known set of valid states.

Why Do Enums Exist?

Enums solve several important problems in software systems.

1. Prevent Invalid States

Without enums:

order.status = "asdfgh"

This may accidentally enter the database and corrupt business logic.

Enums restrict allowed values:

enum status: {
pending: 0,
processing: 1,
completed: 2
}

Now Rails only allows known states.

2. Improve Readability

Compare:

if order.status == 2

vs

if order.completed?

Enums convert meaningless numbers into expressive business language.

3. Save Storage Space

Integers are smaller and faster than strings.

Instead of storing:

"processing"

the DB stores:

1

This improves:

  • indexing
  • query performance
  • storage efficiency

4. Standardize State Management

Enums centralize valid states:

Order.statuses

returns:

{
"pending" => 0,
"processing" => 1,
"completed" => 2
}

This becomes a single source of truth.

5. Enable Better APIs & DSLs

Rails automatically generates methods:

order.pending?
order.completed!
Order.processing

Enums create expressive domain APIs.

How Enums Differ From Other Data Structures

Enums are NOT collections like arrays or hashes.

They represent a finite state system.

🔹 Enum vs Array

Array:

statuses = ["pending", "paid", "failed"]

Problem:

  • no constraints
  • no semantic meaning
  • no mapping behavior
  • no helper methods

🔹 Enum vs Hash

Hash:

STATUSES = {
pending: 0,
paid: 1
}

Closer, but still missing:

  • validations
  • query scopes
  • state predicates
  • DSL methods

Rails enums internally use hashes, but add behavior around them.

🔹 Enum vs Constants

Constants:

PENDING = 0
PAID = 1

Problem:

  • scattered
  • harder to manage
  • no grouped state semantics

Enums organize states cohesively.

🌍 Are Enums Related Only to SQL or Databases?

❌ Absolutely not.

Enums exist in:

  • C
  • Java
  • Rust
  • Swift
  • TypeScript
  • GraphQL
  • Operating systems
  • Compilers
  • APIs
  • State machines

Enums are a general programming concept, not a database feature.

Example: TypeScript Enum

enum Status {
Pending,
Processing,
Completed
}

Example: Java Enum

enum Status {
PENDING,
PROCESSING,
COMPLETED
}

Example: PostgreSQL Native Enum

CREATE TYPE status AS ENUM (
'pending',
'processing',
'completed'
);

This is database-level enum support.

🏗️ How Rails Implements Enums

Rails provides:

ActiveRecord::Enum

located in:

activerecord/lib/active_record/enum.rb

When you write:

class Order < ApplicationRecord
enum status: {
pending: 0,
processing: 1,
completed: 2
}
end

Rails dynamically generates:

1️⃣ Attribute Mapping

order.status
# => "pending"

Internally stored as:

0

in the database.

2️⃣ Predicate Methods

order.pending?
order.completed?

3️⃣ Bang Methods

order.completed!

Equivalent to:

order.update!(status: :completed)

4️⃣ Query Scopes

Order.pending
Order.completed

Generated automatically.

5️⃣ Mapping Helpers

Order.statuses

Returns:

{
"pending" => 0,
"processing" => 1,
"completed" => 2
}

How Rails Maps Enum Values to Integers

Internally Rails stores:

{
pending: 0,
processing: 1,
completed: 2
}

When assigning:

order.status = :processing

Rails converts:

:processing -> 1

before writing to DB.

When reading:

1 -> "processing"

This conversion is handled through ActiveRecord attribute type casting.

Database Example

Ruby:

order.status
# => "completed"

Actual DB value:

status = 2

Why Integers Are Commonly Used

Integers:

  • are compact
  • index efficiently
  • compare faster
  • are DB-friendly

This is why Rails originally used integer-backed enums.

Important Enum Pitfall: Order Matters

This is VERY important.

Dangerous

enum status: [:pending, :processing, :completed]

Rails maps automatically:

pending -> 0
processing -> 1
completed -> 2

If you later insert:

[:pending, :draft, :processing, :completed]

Everything shifts:

  • processing becomes 2
  • completed becomes 3

💥 Existing DB data breaks.

Correct (recommended)

Always use explicit mapping:

enum status: {
pending: 0,
processing: 1,
completed: 2
}

String-Based Enums in Rails

Rails also supports string-backed enums:

enum status: {
pending: "pending",
completed: "completed"
}

Benefits:

  • human-readable DB values
  • safer migrations
  • easier debugging

Tradeoff:

  • slightly larger storage
  • slightly slower indexing

🧪 Real SQL Generated by Rails Enum Queries

Order.completed

Generates:

SELECT *
FROM orders
WHERE status = 2;

Even though Ruby code uses names, SQL uses integers.

🔬 Internals: How ActiveRecord::Enum Works

Internally Rails:

  • stores mappings in a class hash
  • defines methods dynamically using metaprogramming
  • hooks into ActiveRecord attribute casting
  • builds scopes automatically

Rails essentially does something conceptually like:

define_method("completed?") do
status == "completed"
end

and:

scope :completed, -> { where(status: 2) }

This is why enums feel “magical.”

🚨 Limitations of Rails Enums

Enums are useful, but not perfect.

1. Hard to evolve complex workflows

If states become complicated:

pending -> approved -> shipped -> refunded -> disputed

you may need:

  • state machines
  • workflow engines

Examples:

  • aasm
  • state_machines

2. Integer values can become opaque

DB shows:

status = 2

Harder to debug directly.

3. No DB-level validation by default

Rails validates at app layer, but DB still accepts:

status = 999

unless constrained.

🛡️ Best Practices for Rails Enums

Use explicit mappings

enum status: {
pending: 0,
processing: 1,
completed: 2
}

Add DB constraints if critical

Example PostgreSQL constraint:

CHECK (status IN (0,1,2))

Keep enums focused

Good:

status
payment_state
visibility

Bad:

everything_state

Prefer string enums when readability matters

Especially in:

  • analytics-heavy apps
  • debugging-heavy systems
  • APIs

Consider state machines for complex transitions

Enums represent states.
State machines represent transitions.

Very different concepts.

Mental Model Every Developer Should Remember

Think of enums as:

“A controlled vocabulary for state.”

Enums are:

  • not collections
  • not just DB mappings
  • not Rails-specific

They are a way to model finite, meaningful states safely and expressively.

Final Takeaway

Enums exist because software systems constantly need to represent a limited set of valid states in a way that is:

  • efficient
  • readable
  • maintainable
  • safe

Rails’ ActiveRecord::Enum builds a powerful abstraction on top of simple integer (or string) mappings, generating expressive APIs, query scopes, and validations automatically through Ruby metaprogramming.

Understanding enums deeply helps developers:

  • design better domain models
  • avoid fragile state systems
  • write safer queries
  • reason about application workflows more clearly

Enums may look small, but they are one of the foundational building blocks of robust application design.

Happy Implementing! 🚀

GCP Cloud SQL Disaster Recovery: A Practical Guide for Developers

When a production database goes down – whether from a bad migration, an accidental DROP TABLE, or a rogue script – the clock starts ticking. Every minute of downtime is lost revenue, broken trust, and a very stressful Slack channel.

This post walks through how Google Cloud SQL’s backup and recovery features work, common disaster scenarios, and the recovery playbook a developer should follow for each. The examples use a typical SaaS application backed by PostgreSQL on Cloud SQL, but the principles apply broadly.

Cloud SQL Backup Fundamentals

Before anything goes wrong, you need to understand what Cloud SQL gives you out of the box and what you need to configure yourself.

Automated Backups

Cloud SQL can take daily automated backups of your instance. These are full snapshots of the entire database and are retained for a configurable window (default 7 days, max 365).

# gcloud: verify automated backups are enabled
gcloud sql instances describe my-instance \
  --format="value(settings.backupConfiguration)"

Key settings to configure:

SettingRecommendationWhy
backupConfiguration.enabledtrueNon-negotiable for production
backupConfiguration.startTimeOff-peak hours (e.g. 04:00 UTC)Minimizes performance impact
backupConfiguration.backupRetentionSettings.retainedBackups14-30Gives you a wider recovery window
backupConfiguration.pointInTimeRecoveryEnabledtrueEnables PITR (see below)
backupConfiguration.transactionLogRetentionDays7How far back PITR can reach

Point-in-Time Recovery (PITR)

Automated backups give you daily snapshots. PITR fills the gaps by continuously archiving write-ahead logs (WAL for PostgreSQL, binary logs for MySQL). This lets you restore to any second within the retention window — not just to the time of the last backup.

# Enable PITR on an existing instance
gcloud sql instances patch my-instance \
  --enable-point-in-time-recovery \
  --retained-transaction-log-days=7

PITR is the single most important setting for disaster recovery. Without it, you lose every write between your last automated backup and the incident.

On-Demand Backups

You can trigger a backup manually before risky operations:

gcloud sql backups create --instance=my-instance \
  --description="pre-migration-backup-2026-04-08"

Rule of thumb: always take an on-demand backup before running migrations, bulk data operations, or any ad-hoc SQL against production.


Disaster Scenarios and Recovery Playbooks

Scenario 1: Accidental Table Drop or Data Deletion

What happened: A developer ran a DROP TABLE or DELETE FROM without a WHERE clause against production. Maybe it was a script meant for staging. Maybe an AI-generated SQL statement was executed without review.

Impact: One or more tables are gone or empty. The application is throwing 500s.

Recovery options:

Option A: PITR (best if available)

Restore to the moment just before the destructive command. You’ll need the approximate timestamp.

# Restore to a clone instance first — never restore directly over production
gcloud sql instances clone my-instance my-instance-recovery \
  --point-in-time="2026-04-08T10:59:00Z"

This creates a new instance with the database state at that exact second. You can then:

  1. Verify the data on the clone
  2. Export the affected tables from the clone
  3. Import them back into the production instance
# Export a specific table from the recovery clone
gcloud sql export sql my-instance-recovery gs://my-bucket/recovery/users-table.sql \
  --database=myapp_production \
  --table=users

# Import into production
gcloud sql import sql my-instance gs://my-bucket/recovery/users-table.sql \
  --database=myapp_production

Option B: Restore from automated backup

If PITR is not enabled, restore the most recent automated backup that predates the incident.

# List available backups
gcloud sql backups list --instance=my-instance

# Restore a specific backup (this overwrites the instance)
gcloud sql backups restore BACKUP_ID --restore-instance=my-instance

Warning: Restoring a backup directly onto your production instance overwrites everything. All writes since that backup are lost. Prefer cloning to a recovery instance first.

The data gap problem:

When you restore from a backup taken at, say, 4:00 AM, but the incident happened at 11:00 AM, you lose 7 hours of data. This is the gap you’ll need to address manually. Common strategies:

  • Application-level event logs: If your app publishes events to a message queue (Kafka, Pub/Sub), you can replay them.
  • Analytics replicas: If you replicate data to BigQuery, Snowflake, or another analytics store, you can query the missing records from there and re-import them.
  • Audit tables: If your application logs changes to an audit table in a separate database, those records survive.
-- Example: querying BigQuery for records created during the gap window
SELECT *
FROM `project.dataset.user_actions`
WHERE created_at BETWEEN TIMESTAMP('2026-04-08 04:00:00', 'America/Vancouver')
  AND TIMESTAMP('2026-04-08 11:00:00', 'America/Vancouver')
  AND action_type = 'account_status_change'

You then re-ingest these records into production, typically via a script run in your application’s console or through a migration task.


Scenario 2: Interrupted Background Job

What happened: A critical scheduled job — say, one that generates weekly records for all active users — was running when the incident occurred. The database was restored from backup, but the job was killed mid-execution. Some users got their records; others didn’t.

Impact: No application errors (the data that exists is valid), but there’s a silent gap. Some users are missing records they should have.

Recovery playbook:

Step 1 — Quantify the gap

Before doing anything, measure what’s missing:

# Find users who should have a record but don't
target_date = Date.parse('2026-05-30')
users_missing = User.where(status: ['active', 'subscribed'])
  .where.not(id: WeeklyRecord.where(week_date: target_date).select(:user_id))
users_missing.count

Record the count. You’ll need it for verification later.

Step 2 – Understand the generation logic

Before re-running anything, understand what the job does:

  • Does it check for existing records before creating? (idempotent?)
  • Does it behave differently based on user status? (e.g., suspended users get a different treatment)
  • Does it trigger side effects? (emails, webhooks, billing)

If the job is idempotent — meaning running it twice for the same user produces the same result without duplicates — you can safely re-run it for all users, not just the ones missing records. This is simpler and safer than trying to target only the gap.

Step 3 – Re-run with guardrails

Write a targeted script rather than re-triggering the entire job:

target_date = Date.parse('2026-05-30')
# Pre-check
baseline_count = WeeklyRecord.where(week_date: target_date).count
puts "Records before: #{baseline_count}"
# Find and process missing users
users_missing = User.where(status: ['active', 'subscribed'])
.where.not(id: WeeklyRecord.where(week_date: target_date).select(:user_id))
puts "Users missing records: #{users_missing.count}"
users_missing.find_each do |user|
WeeklyRecordGenerator.new(user).generate(target_date)
rescue => e
puts "Failed for User ##{user.id}: #{e.message}"
end
# Post-check
new_count = WeeklyRecord.where(week_date: target_date).count
puts "Records after: #{new_count}"
puts "Delta: #{new_count - baseline_count}"

Step 4 – Verify

Check that:

  • The record count increased by the expected amount
  • No duplicates were created
  • No users are still missing records
  • Any status-dependent logic was applied correctly (e.g., suspended users got the right treatment)

Scenario 3: Corrupted Data from a Bad Migration

What happened: A migration altered a column type, dropped a constraint, or backfilled data incorrectly. The application is running but producing wrong results.

Impact: Data is present but incorrect. This is often harder to detect than missing data.

Recovery playbook:

  1. Don’t panic-restore. If the app is functional (just producing wrong data), you have time to assess.
  2. Clone to a recovery instance from a backup predating the migration: gcloud sql instances clone my-instance pre-migration-clone \ --point-in-time="2026-04-07T23:00:00Z"
  3. Diff the data between production and the clone to understand exactly what changed: -- Compare row counts SELECT 'production' as source, count(*) FROM production.orders UNION ALL SELECT 'backup' as source, count(*) FROM backup_clone.orders; -- Find rows that differ SELECT p.id, p.amount as prod_amount, b.amount as backup_amount FROM production.orders p JOIN backup_clone.orders b ON p.id = b.id WHERE p.amount != b.amount;
  4. Write a targeted fix rather than a full restore (which would lose post-migration legitimate writes).
  5. Write a rollback migration if the schema change itself was the problem.

Scenario 4: Full Instance Failure

What happened: The Cloud SQL instance is unreachable – maybe a zone outage, maybe accidental instance deletion.

Recovery options:

If the instance still exists (zone outage):

Cloud SQL instances configured for high availability will automatically failover to a standby in another zone. If you don’t have HA enabled:

# Enable HA (requires instance restart)
gcloud sql instances patch my-instance --availability-type=REGIONAL

If the instance was deleted:

Deleted instances can be recovered within a limited window if deletion protection wasn’t bypassed:

# Enable deletion protection
gcloud sql instances patch my-instance --deletion-protection

If truly gone, restore from the most recent backup to a new instance:

gcloud sql instances create my-instance-restored \
--source-backup=BACKUP_ID \
--tier=db-custom-4-16384 \
--region=us-west1

Then update your application’s database connection string to point to the new instance.


Prevention Checklist

The best disaster recovery is the one you never need. Here’s what to set up before things go wrong:

Cloud SQL Configuration

# The production-ready configuration checklist
gcloud sql instances patch my-instance \
--backup-start-time=04:00 \
--enable-point-in-time-recovery \
--retained-transaction-log-days=7 \
--retained-backups-count=30 \
--deletion-protection \
--availability-type=REGIONAL

Operational Practices

1. Never run ad-hoc SQL directly against production

Use a read replica for investigative queries. If you must write, use a transaction with a manual ROLLBACK checkpoint:

BEGIN;

-- Your change here
UPDATE users SET status = 'inactive' WHERE last_login < '2025-01-01';

-- Verify before committing
SELECT count(*) FROM users WHERE status = 'inactive';

-- Only if the count looks right:
COMMIT;
-- Otherwise:
ROLLBACK;

2. Take on-demand backups before risky operations

gcloud sql backups create --instance=my-instance \
--description="pre-bulk-update-$(date +%Y%m%d-%H%M%S)"

3. Review AI-generated SQL before executing

AI tools are excellent at generating SQL, but they don’t understand your data invariants. A syntactically correct DROP TABLE or DELETE without a WHERE clause is still catastrophic. Always:

  • Read the generated SQL line by line
  • Run it on staging first
  • Wrap destructive operations in a transaction
  • Have a second pair of eyes for DDL changes

4. Maintain an analytics replica

Replicate critical tables to BigQuery or another analytics store. This serves as both an analytics platform and a recovery source. If your primary database loses data, you can query the replica for the gap window and re-ingest.

# Set up a BigQuery data transfer from Cloud SQL
bq mk --transfer_config \
--target_dataset=sql_replica \
--display_name="Production SQL Replica" \
--data_source=scheduled_query \
--schedule="every 1 hours"

5. Use IAM to restrict destructive operations

Not every developer needs cloudsql.instances.delete or direct SQL access to production:

# Create a read-only role for most developers
gcloud projects add-iam-policy-binding my-project \
--member="group:developers@company.com" \
--role="roles/cloudsql.viewer"
# Grant write access only to the ops team
gcloud projects add-iam-policy-binding my-project \
--member="group:database-ops@company.com" \
--role="roles/cloudsql.admin"

The Recovery Timeline: What Happens in Practice

Here’s what a real recovery typically looks like, end to end:

T+0min Incident detected (alerts fire, app errors spike)
T+5min Confirm the issue — is it a code bug or data loss?
T+10min Identify the last good backup / PITR target
T+15min Clone instance from backup (takes 5-30 min depending on size)
T+45min Verify restored data on the clone
T+60min Restore production from clone or selectively import tables
T+90min Identify the data gap (writes between backup and incident)
T+120min Query analytics replica / event logs for gap data
T+150min Re-ingest gap data, verify counts
T+180min Re-run interrupted jobs with verification
T+210min Final validation — all counts match, no duplicates, app healthy
T+240min Post-incident review

The total time depends on database size, gap complexity, and whether you had PITR enabled. With PITR, the gap is seconds. Without it, you could be looking at hours of manual data reconciliation.


Key Takeaways

  1. Enable PITR. It’s the difference between losing seconds of data and losing hours.
  2. Always clone to a recovery instance first. Never restore directly over production unless you have no other option.
  3. Maintain an analytics replica. It’s your insurance policy for the data gap.
  4. Quantify before you fix. Record counts before and after every recovery step. You can’t verify what you didn’t measure.
  5. Understand your jobs’ idempotency. If a background job was interrupted, knowing whether it’s safe to re-run is the difference between a smooth recovery and creating a bigger mess.
  6. Take on-demand backups before risky operations. The 30 seconds it takes could save you 4 hours of recovery.
  7. Review all SQL before execution. Especially AI-generated SQL. Trust, but verify.

Production incidents are stressful, but with the right configuration and a clear playbook, they don’t have to be catastrophic. Set up your backups today — future you will be grateful.

Happy fixing!


Mastering RSpec Test Doubles in Rails 7+ (Ruby 3+)

When writing tests in RSpec, especially in modern Rails 7+ apps with Ruby 3+, understanding test doubles, stubs, and mocks is essential for writing clean, fast, and maintainable tests.

In this guide, we’ll break down:

  • What are doubles, stubs, and mocks
  • When to use each
  • Common RSpec methods (let, let!, subject, allow, expect)
  • Real-world Rails examples (controllers, services, serializers)
  • Best practices and pitfalls

Why do we need test doubles?

In real applications, your code interacts with:

  • External APIs
  • Databases
  • Background jobs
  • Third-party services (Stripe, Redis, etc.)

Testing all of these directly makes tests:

  • Slow
  • Fragile
  • Hard to isolate

Test doubles solve this by replacing real dependencies with controlled, predictable behavior.


1. Test Double – The Foundation

What is a double?

A double is a fake object that stands in for a real one.

let(:user) { double('User', name: 'Adam') }
it 'returns user name' do
expect(user.name).to eq('Adam')
end

instance_double (Recommended)

let(:user) { instance_double(User, name: 'Adam') }

Why better?

  • Verifies methods exist on real class
  • Prevents typos

Rule:

Use instance_double over double whenever possible


2. Stub — Controlling Behavior

What is a stub?

A stub defines what a method should return.

allow(user).to receive(:admin?).and_return(true)

You are saying:

“If admin? is called, return true.”


Rails Example

class DiscountService
def initialize(user)
@user = user
end
def call
@user.admin? ? 50 : 10
end
end

Spec:

describe DiscountService do
let(:user) { instance_double(User) }
it 'returns 50 for admin user' do
allow(user).to receive(:admin?).and_return(true)
result = described_class.new(user).call
expect(result).to eq(50)
end
end

Key idea:

  • Stub = control output
  • Does NOT verify method is called

3. Mock — Verifying Behavior

What is a mock?

A mock verifies that a method was called.

expect(service).to receive(:call)

You are saying:

“This method MUST be called.”

Rails Example (Service interaction)

class OrderProcessor
def initialize(payment_gateway)
@payment_gateway = payment_gateway
end
def call(amount)
@payment_gateway.charge(amount)
end
end

Spec:

describe OrderProcessor do
let(:gateway) { instance_double('PaymentGateway') }
it 'charges the payment gateway' do
expect(gateway).to receive(:charge).with(1000)
described_class.new(gateway).call(1000)
end
end

Key idea:

  • Mock = verify interaction
  • Test fails if method is NOT called

4. Stub + Real Method → .and_call_original

Hybrid approach

expect(User).to receive(:find).and_call_original

Meaning:

  • Verify method is called ✅
  • Execute real implementation ✅

Rails Example

expect(Serializers::ProductInfo).to receive(:new).with(
product: product,
date: Date.today
).and_call_original

Use carefully:

  • Tests implementation, not behavior
  • Can become brittle

5. let vs let!

let (lazy)

let(:user) { create(:user) }
  • Runs only when used

let! (eager)

let!(:user) { create(:user) }
  • Runs before each test

Example

let!(:recipes) { create_list(:recipe, 3) }
it 'returns recipes' do
get '/recipes'
expect(JSON.parse(response.body).size).to eq(3)
end

Rule:

  • Use let by default
  • Use let! when DB setup must happen before request

6. subject — Defining the Action

subject(:request) { get '/api/v1/home/homepage' }

Usage

it 'returns 200' do
request
expect(response).to have_http_status(:ok)
end

Benefits:

  • Reusable
  • Lazy
  • Override in contexts

7. allow_any_instance_of (⚠️ Avoid if possible)

allow_any_instance_of(User).to receive(:admin?).and_return(true)

Problem:

  • Affects ALL instances
  • Hard to debug
  • Breaks isolation

Better:

allow(user).to receive(:admin?).and_return(true)

8. Real Rails Example (Controller + Service)

Controller

class OrdersController < ApplicationController
def create
order = OrderBuilder.new(params).create
render json: { id: order.id }
end
end

Spec

describe 'POST /orders' do
let(:mock_order) { instance_double(Order, id: 123) }
let(:builder) { instance_double(OrderBuilder) }
before do
allow(OrderBuilder).to receive(:new).and_return(builder)
allow(builder).to receive(:create).and_return(mock_order)
end
it 'returns order id' do
post '/orders', params: { name: 'Test' }
expect(response).to have_http_status(:ok)
expect(JSON.parse(response.body)['id']).to eq(123)
end
end

Summary Table

ConceptMethodPurpose
Doubledouble, instance_doubleFake object
Stuballow(...).to receiveControl return value
Mockexpect(...).to receiveVerify method call
Hybrid.and_call_originalVerify + run real code
Lazy setupletRun when needed
Eager setuplet!Run before test
ActionsubjectDefine main execution

Common Pitfalls

Over-mocking

  • Tests break on refactor
  • Tests implementation, not behavior

Using allow_any_instance_of

  • Global side effects
  • Avoid unless absolutely necessary

Too many let!

  • Slower tests
  • Hidden setup

Best Practices

  • Prefer behavior testing over implementation
  • Use instance_double instead of double
  • Keep tests readable like English
  • Use shared_context for repeated setup
  • Avoid overusing mocks

Final Thought

Think of RSpec like this:

  • Double → Fake object
  • Stub → “Return this value”
  • Mock → “This must be called”

Mastering these will make your Rails tests:

  • Faster ⚡
  • Cleaner 🧼
  • More reliable 🧪

Happy Testing!