Rails 6 is here!

On , by

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#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
  self.abstract_class = true

  connects_to database: { writing: :animals_primary, reading: :animals_replica }
end

class Dog < AnimalsModel
  # connected to both the animals_primary db for writing and the animals_replica for reading
end

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

development:
  primary:
    database: my_primary_db
    user: root
  primary_replica:
    database: my_primary_db
    user: ro_user
    replica: true
  animals:
    database: my_animals_db
    user: root
  animals_replica
    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#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.

COMMENTS_REGEX = /^comment\+(.+)@example\.com/i

# app/mailboxes/application_mailbox.rb
class ApplicationMailbox < ActionMailbox::Base
  routing COMMENTS_REGEX => :comments
end

# app/mailboxes/comments_mailbox.rb
class CommentsMailbox < ApplicationMailbox
  def process
    user = User.find_by(email: mail.from)
    post_uuid = COMMENTS_REGEX.match(mail.to)[1]

    post = Post.find_by(uuid: post_uuid)
    post.comments.create(user: user, content: mail.body)
  end
end

Action Text: Handling Rich Text Content#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
end

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 %>
  </div>
<% 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.

Share this article on Twitter or LinkedIn
Previous Blog Post: Dummies Guide to Delegation in Swift
Subscribe to Sudo vs Root

Our newsletter rolls out every month. No fluff. Pure content.

Comments and Discussions
Skcript https://www.skcript.com/svr/rails-6-is-here/ https://www.skcript.com/svrmedia/heroes/rails-6-is-here@1.5x.png