What's new with Ruby on Rails 7

Posted by Karan Shah · 18 Jan, 2022 · 5 Min read
What's new with Ruby on Rails 7
Outline
Outline

As of Jan 2022, Ruby on Rails has been used in over 408,599 global websites, including WeTransfer.com, Shopify and Airbnb.

Ruby on Rails is proficient in building everything from simple websites to globally renowned sites that receive humongous traffic. This web application framework comprises 6% of all the websites whose server-side programming language we know.

Ruby on Rails was first developed in 2005 as open-source software to help developers build web applications. Over the past 17 years, the app web development framework has released over 6 versions of Ruby on Rails, and the next one is also up now. Unlike other web development frameworks, Ruby focuses on making coding non-repetitive and simple.

The latest version of Ruby on Rails has some extraordinary features and updates that will simplify the web development process.

What's new in the latest Ruby on Rails 7?

The latest version of Ruby on Rails has some extraordinary and exciting features. Rails creator David Hansson that this version brings everything needed for the developers to create a modern web app. Hanson said 7.0 is "all the cards are on the table. No more fooling our sleeves. The culmination of many years of progress in five different aspects at once.". Let's see what you will find the Ruby on Rails 7-

1. Webpack and Node are not required.

Developers need not require Webpack and Nodejs to use npm packages. Transpiling ES6 and Babel and then bundling would require a lot of steps. While developers can use Webpacker gem to perform this task, it brought additional baggage and was hard to understand and make changes to.

Developers can now import maps via importmaps-rails gem. Also, instead of writing code for package.json and installing dependencies using yarn or npm, you can use ./bin/importmap to update, pin or unpin dependencies.

For example- to install date-fns:

$ ./bin/importmap pin date-fns

This will automatically include a line in config/importmap.rb like:

in "date-fns", to: "https://ga.jspm.io/npm:date-fns@2.27.0/esm/index.js"

And, in your javascript, you continue to write codes like you used to.

import { formatDistance, subDays } from 'date-fns'

formatDistance(subDays(new Date(), 3), new Date(), { addSuffix: true })
//=> "3 days ago"

With this structure, you need to keep this in mind because there is no transpiring between what code you write and what the browser perceives. This is ok because most browsers that matter now support ES6. Only JSK and Typescript require transpiration to JS before use.

So, for instance, if you want to use React with JSX, developers have to use the traditional setup of using webpack, esbuild and rollup.

Rail 7 can make this fast for you. All you need is to use the following command with any of the chosen strategies-

$ ./bin/rails javascript:install:[esbuild|rollup|webpack]

2. Encrypted Database Layer

Rail 7 allows developers to encrypt certain database fields using the encrypts method on the ActiveRecord::Base. This means that after your initial setup, you have to write a code like this-

class Message < ApplicationRecord
encrypts :text
end

The encrypted attributes are like any other attribute. Rails 7 will automatically encrypt and decrypt your application and its database.

But, the catch is that you cannot query the database of the field unless you use the encrypts method to pass the deterministic: true option. However, the deterministic model is less secure than other modes, so only used when there is an emergency.

3. Asynchronous Querying

Developers can now use the load_async method while querying data to fetch results. This is time-savvy when asking multiple queries together. Using this, you can run-

def PostsController
def index
@posts = Post.load_async
@categories = Category.load_async
end
end

This will fire two queries together. So, if each query took you 200ms, the total time fetching two results here would be 200 ms only, not 400 ms.

4. Applications would only run in the Zeitwerk mode.

All Rail 7 applications will now run in the Zeitwerk mode. The management has ensured that this transformation will be smooth for the developers. Zeitwerk is a code loader for Ruby on Rails. The developers can easily load your project's module and classes using this.

This is launched so that any gem dependency, project, application etc., can have its loader. Each loader has its inflector, configuration, and logger. Zeitwerk has all features of the Classic mode; the only difference is that it provides a better strategy for loading elements.

5. UJS and Turbolinks is now replaced Stimulus and Turbo

Applications using Rails 7 will now get Stimulus and Turbo (from Hotwire) from default. Hotwire is a modern approach to building web applications using minimal JavaScript by sending HTML and not JSON over the wire.

This means fast loading pages, templates simultaneously running on the server, and a productive development experience for developers. Turbo is another complementary technique of Hotwire used for speeding up page changes, streamlining complex pages into components and stream updates over WebSockets.

Both Turbo and Hotwire are hybrid technology; they can be integrated into iOS and Android. And Stimulus pairs with Turbo to provide a solution required to build fast and compelling applications with minimal effort.

6. Inline your query with a single record using sole

Developers can now use first or find_by instead of sole or find_sole_by when asserting a query to match a single record.

Product.where(["price = %?", price]).sole
# => ActiveRecord::RecordNotFound (if no Product with given price)
# => #<Product ...> (if one Product with given price)
# => ActiveRecord::SoleRecordExceeded (if more than one Product with given price)
user.api_keys.find_sole_by(key: key)
# as above

7. Use controller actions to the stream generated files.

Rail on Ruby 7 allows you to streamline a file generated on the fly using send_stream inside controller action.

send_stream(filename: "subscribers.csv") do |stream|
stream.write "email_address,updated_at\n"
@subscribers.find_each do |subscriber|
stream.write "#{subscriber.email_address},#{subscriber.updated_at}\n"
end
end

This provides developers with an immediate/ partial response to know that something is happening, which will benefit when deployed on Heroku.

8. Named Variants

You can name variants using ActiveStorage in the latest Ruby on Rails 7.

class User < ApplicationRecord
has_one_attached :avatar do |attachable|
attachable.variant :thumb, resize: "100x100"
end
end
#Call avatar.variant(:thumb) to get a thumb variant of an avatar:
<%= image_tag user.avatar.variant(:thumb) %>

Wrapping up

To get a detailed view of the bug fixes, features and changes made, check out the Rails 7 release note. These aren't very comprehensive but will get updated sometime. If you are running on Rail 6 or lower, then remember that with the final release of Ruby on Rails 7, Rail 6.1 will not receive any bug fixes. You can now update Rail 7 and enjoy the latest updates and features.

Subscribe to The Friday Brunch
Our twice a month newsletter featuring insights and tips on how to build better experiences for your customers
READ ALSO

Have a product idea?

Talk to our experts to see how you can turn it
into an engaging, sustainable digital product.

SCHEDULE A DISCOVERY MEETING