Rails 6 is here!

Ruby on Rails gets better with its 6.0 release. Here are three of our favorite features in this release.

Karthik Kamalakannan

Karthik Kamalakannan

Rails 6 is here!

It's here! Rails 6 seems to be much kinder, after the breaking changes of Rails 5. But it still packs a punch. It focuses a lot on speed and scalability. There's also some nifty features borrowed from Basecamp.

Here's my favorite Rails 6 features.

Active Record: Connection Switching to Support Multiple Databases

This is crazy! When I read about it I was like "finally!". With connection switching your Active Record model can connect to multiple databases. This allows for a primary "write" database and a replica "read" database.

class AnimalsModel < ApplicationRecord
  connects_to database: { writing: :animals_primary, reading: :animals_replica }
class Dog < AnimalsModel
  # connected to both the animals_primary db for writing and the animals_replica for reading

How great is that! To make this happen, just add the required configuration to your database.yml.

    database: my_primary_db
    user: root
    database: my_primary_db
    user: ro_user
    replica: true
    database: my_animals_db
    user: root
    database: my_animals_db
    user: ro_user
    replica: true

Courtesy: Guy Maliar's, "What's coming to Rails 6.0?"

Action Mailbox: Receive Emails Within A Rails Application

I'm sure this is a feature that will be spoken about up until Rails 7 is out! It certainly is one of the most exciting.

Action Mailbox routes incoming emails to a controller like interface. There's support for SendGrid, Mailgun, Mandrill and more right out of the box.

All incoming emails are turned into InboundEmail records that store life-tracking information and also the original email as via ActiveStorage.

InboundEmail records will automatically be removed after 30 days. The idea being that all required information is extracted into other domain models by that time. And also it's all about that data privacy, it's 2019 mate!

Here's a small snippet that shows how you can listen to incoming emails from a user and add it as a comment from them.

# app/mailboxes/application_mailbox.rb
class ApplicationMailbox < ActionMailbox::Base
  routing COMMENTS_REGEX => :comments
# app/mailboxes/comments_mailbox.rb
class CommentsMailbox < ApplicationMailbox
  def process
    user:User.find_by(email: mail.from)
    post:Post.find_by(uuid: post_uuid)
    post.comments.create(user: user, content: mail.body)

Action Text: Handling Rich Text Content

Action Text is a wonderful port of Trix editor by Basecamp right onto Rails 6.

With Action Text, adding a text editor is super easy. And since it's Rails, it includes conventions for forms, Active Record and Active Storage.

You will first need to run rails action_text:install to add the Yarn package and copy over the necessary migration. Also, you need to set up Active Storage for embedded images and other attachments.

Let's look at some code!

Add has_rich_text to the preferred attribute on your ActiveRecord model.

# app/models/message.rb
class Message < ApplicationRecord
  has_rich_text :content

Now let's look at the form helper to support Action Text.

<%# app/views/messages/_form.html.erb %>
<%= form_with(model: message) do |form| %>
  <div class="field">
    <%= form.label :content %>
    <%= form.rich_text_area :content %>
<% end %>

How nifty is that rich_text_area function? It expands into a WYSIWYG HTML editor!

Now to display a sanitized version of the saved text, just do

<%= @message.content %>

So simple!

There's a bunch of new features that Rails 6 introduces. Read about all of them on the official Release Notes page. And write to me about your favorite feature, just tweet to me at @imswaathik.

Last updated: November 21st, 2023 at 6:56:16 PM GMT+0


Get notified about new updates on Product Management, Building SaaS, and more.

Skcript 10th Anniversary

Consultants for ambitious businesses.

Copyright © 2023 Skcript Technologies Pvt. Ltd.