Ruby on Rails 8 Unleashed: Game-Changing Features Every Developer Will Love!

Raghavendra S
4 min readNov 4, 2024

Get ready for a revolution in web development with Ruby on Rails 8! From blazing-fast concurrent queries to powerful frontend components and cutting-edge security, Rails 8 is packed with features that will transform the way you build apps. Dive into this detailed guide complete with code snippets and discover how Rails 8 is designed to make your projects faster, cleaner, and more secure. Buckle up and prepare to fall in love with Rails all over again!

1. Concurrent Active Record Queries: Speed Things Up!

Rails 8 has taken a big step forward by introducing concurrent queries in Active Record. This feature allows you to execute multiple database queries simultaneously, cutting down on waiting time and making your app more efficient.

Example:

Imagine needing to fetch data from multiple tables. Previously, you’d have to wait for each query to complete sequentially. Now, you can run them concurrently!

# Old way: Sequential queries
user = User.find_by(id: params[:id])
posts = Post.where(user_id: user.id)
comments = Comment.where(user_id: user.id)

# New way: Concurrent queries
user, posts, comments = ActiveRecord::Future.all(
User.find_by(id: params[:id]),
Post.where(user_id: params[:id]),
Comment.where(user_id: params[:id])
)

Result: Faster database calls, happier users, and a more performant app!

2. Component-Based Frontend Development

Rails 8 embraces a more modern approach to frontend architecture with built-in support for components. This makes it easy to create reusable, isolated UI elements. If you’ve used frameworks like React or Vue, you’ll feel right at home.

Defining a Component:

Using the new ViewComponent library, here’s how you can create a button component:

# app/components/button_component.rb
class ButtonComponent < ViewComponent::Base
def initialize(label:, color: "primary")
@label = label
@color = color
end
end
<!-- app/components/button_component.html.erb -->
<button class="btn btn-<%= @color %>">
<%= @label %>
</button>

Using the Component:

<%= render(ButtonComponent.new(label: "Click Me", color: "success")) %>

Why This Matters: Components make your views cleaner, more organized, and easier to maintain. Plus, they encourage reusability across your app.

3. Turbo and Stimulus 3.0: Less JavaScript, More Magic

Rails 8 doubles down on the Hotwire framework, integrating Turbo and Stimulus 3.0 for building fast and interactive apps. You get real-time updates and dynamic behavior with minimal JavaScript.

Turbo Streams:

Updating parts of your page in real time is now a breeze. Want to broadcast new messages to all users? Check this out:

# app/models/message.rb
class Message < ApplicationRecord
after_create_commit { broadcast_append_to "messages" }
end
<!-- app/views/messages/index.html.erb -->
<div id="messages">
<%= turbo_stream_from "messages" %>
<%= render @messages %>
</div>

Turbo Frames:

Load parts of a page asynchronously without a full page reload. Here’s an example:

<!-- Using Turbo Frame to load user details -->
<turbo-frame id="user_details">
<%= render @user %>
</turbo-frame>

Why Use This? Hotwire lets you build rich, interactive apps without the complexity of a full-blown JavaScript framework. Less code, fewer headaches!

4. Security Enhancements: Stay Safe by Default

Rails 8 comes with upgraded security features to keep your app safe. New measures include stricter protections against common vulnerabilities like XSS and CSRF, and support for Content Security Policy (CSP).

Enforcing Content Security Policy:

You can define your CSP in an initializer:

# config/initializers/content_security_policy.rb
Rails.application.config.content_security_policy do |policy|
policy.default_src :self, :https
policy.script_src :self, :https, :unsafe_inline
policy.style_src :self, :https
end

Takeaway: Security is baked in, so you can focus on building your app while Rails takes care of the heavy lifting.

5. Active Record Encryption Made Easier

Encrypting sensitive data has never been simpler. Rails 8 enhances Active Record encryption with better key management and improved performance.

Encrypting a Column:

# Migration to add an encrypted column
class AddEncryptedEmailToUsers < ActiveRecord::Migration[6.0]
def change
add_column :users, :encrypted_email, :string
end
end

# Encrypting the column in the model
class User < ApplicationRecord
encrypts :email
end

Why Encrypt? In today’s world, data security is a must. Rails makes it easy to keep sensitive information safe.

6. Asynchronous Queries: Making Your App More Responsive

Rails 8 introduces asynchronous queries, allowing you to load data in the background without blocking your main thread. This is a game-changer for apps with heavy database operations.

Using Async Queries:

# Fetch data asynchronously
posts = Post.async_load
users = User.async_load

# Wait for results when needed
posts.wait
users.wait

Pro Tip: Use async queries to improve the user experience by fetching data in parallel.

7. Updated Rails CLI: A Dev’s Best Friend

The Rails Command Line Interface (CLI) gets a facelift with more intuitive commands and better options. Generating new projects and debugging issues has never been easier.

Creating a New Project:

rails new my_app --database=postgresql --css=tailwind

Debugging Made Simple:

Rails 8 offers clearer error messages, making it easier to troubleshoot issues.

Why This Update Rocks: Faster setup, less confusion, and a more developer-friendly experience.

Wrapping It Up

Rails 8 is all about making web development faster, more efficient, and enjoyable. Whether it’s performance improvements with concurrent queries, modern frontend practices, or enhanced security, this release has something for everyone.

Ready to upgrade and experience the magic? Let’s get building!

What feature are you most excited about in Rails 8? Drop your thoughts in the comments below!

Happy coding, fellow Rails lovers!

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Raghavendra S
Raghavendra S

Written by Raghavendra S

Artificial enthusiast. Rubyist.

Responses (7)

Write a response

why is async_load presented as part of rails 8? it’s already used in previous versions

--

I appreciate the effort, but a lot of AI slop.

--

Rails 8 embraces a more modern approach to frontend architecture with built-in support for components

Rails 8 does not have ViewComponent. It is an external dependency

--