DEV Community

Cover image for 10 Advanced Ruby on Rails Features
Colin Soleim
Colin Soleim

Posted on

10 Advanced Ruby on Rails Features

I recently saw a great article posted by an engineer about advanced tips and tricks in Python (https://blog.edward-li.com/tech/advanced-python-features/) which got me thinking about similar features in Ruby on Rails.

Even after 10 years working inside large Rails codebases, here's some features that I've discovered recently working with our clients from NextLink Labs (https://nextlinklabs.com) that make Rails faster, safer, and a lot more fun. Below are my top ten, each paired with a minimal example you can drop into any 6.1 + app (most work on 7.0–7.2).


Table of Contents

  1. Strict Loading
  2. Asynchronous Queries
  3. Multi-Database Connection Switching & Sharding
  4. Delegated Types
  5. Active Record Encryption
  6. Attributes API & Custom Types
  7. Turbo-Stream Broadcasting from Models
  8. Built-in Rate Limiting
  9. One-Command Dev Containers
  10. Ignoring Counter Caches While Backfilling
  11. Default GitHub CI Workflow
  12. YJIT as the Default JIT Engine
  13. Browser Version Guard
  14. Out-of-the-Box PWA Files

1. Strict Loading

Raise an error the moment an association lazy-loads, catching N + 1 problems while you code rather than in production.

class Article < ApplicationRecord strict_loading_by_default end 
Enter fullscreen mode Exit fullscreen mode

2. Asynchronous Queries

Off-load expensive SQL to a background pool while the request keeps executing.

posts = Post.published.order(created_at: :desc).load_async authors = Author.popular.limit(10).load_async 
Enter fullscreen mode Exit fullscreen mode

When you finally call posts.each, the records are already waiting in memory.


3. Multi-Database Connection Switching & Sharding

Read-replica traffic and horizontal shards are first-class citizens.

AnimalsRecord.connected_to(role: :reading) { Report.last } TenantsRecord.connected_to(shard: :europe) do Invoice.create!(...) end 
Enter fullscreen mode Exit fullscreen mode

4. Delegated Types

A clean alternative to both STI and polymorphic associations.

class Entry < ApplicationRecord delegated_type :entryable, types: %w[Post Comment] end 
Enter fullscreen mode Exit fullscreen mode

Entry.first.entryable transparently returns the concrete model with zero casting or multi-table joins.


5. Active Record Encryption

Transparent, per-attribute encryption baked into Rails 7.

class User < ApplicationRecord encrypts :ssn, deterministic: true end 
Enter fullscreen mode Exit fullscreen mode

Rails handles key rotation, blind-index search, and log filtering—no extra gems required.


6. Attributes API & Custom Types

Cast arbitrary data into Ruby objects without virtual-attribute hacks.

class MoneyType < ActiveModel::Type::Integer def serialize(value) = (value.to_f * 100).to_i def deserialize(value) = value.to_i / 100.0 end class Product < ApplicationRecord attribute :price, MoneyType.new end 
Enter fullscreen mode Exit fullscreen mode

Validations, serializers, and forms treat price as a float while the database stores cents.


7. Turbo-Stream Broadcasting from Models

Real-time UI updates with a single callback.

class Message < ApplicationRecord after_create_commit -> { broadcast_append_to :chat } end 
Enter fullscreen mode Exit fullscreen mode

Every new message pushes a <turbo-stream> fragment to subscribed browsers—no controller code, no JavaScript.


8. Built-in Rate Limiting

Rails 7.2 ships with rate_limit, ending the reign of Rack-Attack.

class Api::SessionsController < ActionController::API rate_limit to: 10, within: 1.minute, only: :create end 
Enter fullscreen mode Exit fullscreen mode

Exceeding the limit triggers an automatic HTTP 429.


9. Browser Version Guard

Block outdated browsers via a declarative whitelist.

class ApplicationController < ActionController::Base allow_browser safari: "16", chrome: "110", firefox: "112" end 
Enter fullscreen mode Exit fullscreen mode

10. Out-of-the-Box PWA Files

Rails 7.2 generators include a manifest and service-worker template under /pwa, making an installable, offline-capable Progressive Web App a few edits away.


Conclusion

That’s a whirlwind tour through some more advanced features that I've learned and found useful in my recent work. How many were new to you? Drop me a note—I’d love to hear about your examples of useful Rails features.

If you're struggling with a legacy Rails app or looking for any kind of help with your application, feel free to send us a message here (https://nextlinklabs.com/contact)

You might also find some of my other blog posts useful:

Common Types of Rails Technical Debt

Six Tips For Inheriting a Legacy Rails App

Top comments (0)