A new version of Rails is just around the corner. Currently, there is no release date confirmed, but it is expected to be available before Christmas. As of the publication of this post, the most recent version is 7.0.0.rc1. HEY, Github, Shopify, and Basecamp all run the Rails 7 alpha in production, so we can expect even the release candidate to be stable.
Our goal in this post is to look at some of the new features and changes that Rails 7 will bring.
Is there anything new in Ruby on Rails 7?
There are some extraordinary and exciting features in the latest Ruby on Rails version. According to Rails creator David Hansson, this version includes everything developers need to create a modern web application. This release is the culmination of many years of progress in five different areas at once, according to Hanson. Let’s take a look at what Ruby on Rails 7 has to offer
1. There is no need for Webpack or Node.
It is not necessary for developers to have Webpack and Nodejs to use npm packages. A lot of steps would be required to translate ES6 and Babel and then bundle them. Webpacker gem can be used for this task, but it brought additional baggage and was difficult to modify.
The importmaps-rails gem now allows developers to import maps. To update, pin, or unpin dependencies, you can use ./bin/importmap instead of writing code for package.json.
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:[email protected]/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”
When working with this structure, you must keep this in mind because what you write does not transpire to what the browser perceives. Since most browsers now support ES6, this is not a problem. Transformation to JS is required only for JSK and Typescript.
For example, developers have to use webpack, esbuild, and rollup if they want to use React with JSX.
This can be done quickly with Rail 7. With any of the chosen strategies, simply execute the following command:
$ ./bin/rails javascript:install:[esbuild|rollup|webpack]
2. A database layer that is encrypted
With Rail 7, developers can encrypt certain database fields using the encrypts method on ActiveRecord::Base. You need to write the following code after you have set up your initial setup:
class Message < ApplicationRecord
encrypts:text
end
There is no difference between encrypted attributes and other attributes. The Rails 7 framework automatically encrypts and decrypts your database and application.
However, if you pass the deterministic: true option to the encrypts method, you cannot query the field’s database. As a result, the deterministic model is less secure than other modes, so it is typically used in emergencies only.
3. Asynchronous Querying
The load_async method is now available to developers when querying data. When multiple queries are asked together, this is time-saving. You can run the following using this:
def PostsController
def index
@posts = Post.load_async
@categories = Category.load_async
end
end
Two queries will be fired at the same time. In other words, if each query took 200ms, the total time to fetch two results would be 200ms instead of 400ms.
4. It would only be possible to run applications in Zeitwerk mode.
The Zeitwerk mode of Rail 7 will now be available for all applications. It has been ensured by the management that this transformation will be smooth for the developers. Ruby on Rails code loader Zeitwerk. It allows developers to easily load modules and classes from your project.
It is used to launch gem dependencies, projects, applications, etc. Inflectors, configurations, and loggers are all unique to each loader. The only difference between Zeitwerk and Classic mode is that it provides a better loading strategy.
5. Now Stimulus and Turbolinks are replacing UJS and Turbolinks
Rails 7 applications now come with Stimulus and Turbo (from Hotwire) by default. In Hotwire, HTML is sent over the wire instead of JSON, enabling developers to build web applications with minimal JavaScript.
As a result, pages load quickly, templates run simultaneously on the server, and developers have a productive development experience. With Turbo, you can speed up page changes, streamline complex pages into components, and stream updates over WebSockets.
Hotwire and Turbo are both hybrid technologies; they can be integrated into iOS and Android devices. Combined with Turbo, Stimulus provides a solution for building fast and compelling applications.
6. Use sole to inline your query with a single record
When asserting that a query matches a single record, developers can now use first or find_by instead of sole or find_sole_by.
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. Stream generated files can be handled by controller actions.
Using send_stream inside a controller action, Rail on Ruby 7 streamlines files generated on the fly.
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
When deployed on Heroku, developers will receive an immediate/partial response so that they know something is happening.
8. Variants with names
Ruby on Rails 7 supports naming variants using ActiveStorage.
Conclusions
In the Rails 7 release notes, you can find the complete list of bug fixes, features, and changes. Currently, they aren’t comprehensive, but they will be updated soon.
Please note that Rails 6.1 will no longer receive bug fixes if you are still running Rails 6 or lower with Rails 7’s final release. The EOL for Rails 5.2 will also mark its end of support, since it will no longer receive any updates.
have fun coding! Contact RailsCarma for more information!