What's New in Rails 7
A quick look at some of the great new features in Rails 7.Asad Ahmed
December 01, 2022Ruby on Rails was initially released back in 2005. Although it doesn't seem like a long time ago, technology advances quickly and can become obsolete equally as quickly. Ruby, a programming language that is nearly ten years older than Rails, is used to create Rails.
Continuous updates are made to Ruby on Rails. To keep the framework current, the Ruby on Rails team and developers from all over the world contribute significant new features. Even better now, The Rails Foundation was launched this year. With a seed fund of a million dollars, the Foundation's mission is to improve documentation, education and the general ecosystem of Rails.
More than six versions of Ruby on Rails have been made available over the course of the last 17 years, and the seventh is currently available. Thanks to the more than 6000 incredible Rails contributors that made this possible.
Ruby places a greater emphasis on making coding straightforward and non-repetitive than other web development frameworks.
In this post I will highlight the latest and most important additions to Ruby on Rails.
Native encryption at database layer (finally!)
This new feature is, I believe, one of the most important new features in Rails 7. Although not particularly insecure, It's a known fact that Rails apps are not the most secure right out of the box.
At Harled, we used Lockbox for modern encryption before this new feature was introduced in Rails 7 and we loved it, BIG thanks from the team at Harled to Andrew Kane. Although gems like Lockbox did a great job and did a lot of heavy lifting, native encryption is easier and safer.
No more adding hashes to models and installing gems. Now, encryption will be supported by ActiveRecord out of the box.
The encrypted characteristics can still be used in the same way as normal attributes. It will be automatically encrypted and decrypted between the database and your application using Rails 7. However, there is a small catch: unless you supply a "deterministic: true" option to the encrypts function, you cannot query the database by that column. Use the deterministic mode only for attributes you absolutely must query because it is less secure than the non-deterministic option that is used by default.
Asynchronous Querying
If you've been using Rails for a while, you'll know that one of the most common complaints has always been that it's slow. Rails is not the leanest framework, in my opinion, however, the heftiness of its features more than makes up for its lack of leanness.
When querying data, you can now use the load async method to retrieve data in the background. This is particularly important when loading multiple unrelated queries from a controller action.
In the code above, both queries will be executed in the background at the same time. So, if each query takes 100ms, the total time spent retrieving data is 100ms rather than 200ms.
However, there are a few caveats to keep in mind when using load_async function. Always consider your database connection pool. The number of queries to the database should be carefully considered based on the size of the database pool. Exceeding the pool size can lead to database connection loss. Increasing the pool size might be the first solution that crosses your mind when dealing with this problem. However, increasing the pool size will slow down query times. This defeats the purpose of the function.
Node and Webpack No More
Webpack is a valuable tool, however, it's not the most lightweight or elegant solution. Although it was quite well supported by Rails with the Webpacker gem, this came with a lot of baggage, was challenging to understand, and was difficult to modify, especially while retaining upgradeability.
Now, using import maps via the importmap-rails gem is the default for new apps made using Rails new. Import maps allows you to map a logical name to a downloaded module directly in the browser without the need for additional bundling on the server for the browser, as well as a Rails wrapper to manage that mapping from your code. However, it will probably take the industry a while to make all packages available.
Webpacker, UJS, and Turbolinks are replaced with Hotwire and Stimulus in Rails 7. This is undoubtedly the release's most exciting feature. This allows you to create reactive web apps with little to no JavaScript.
Retry Jobs will Retry Unlimited Times
A background job can fail for a variety of reasons, including a network error. Prior to Rails 7, there was no way for a job to be retried indefinitely.
ActiveJob now supports "passing:unlimited" as the retry on attempts parameter which prompts Rails to continue to attempt the job until the job has completed.
Wrapping Up
The full list of bug fixes, features, and changes can be found in the Rails 7 release notes. These are not yet complete, but we can expect them to be updated soon.
At Harled, we already started upgrading to Rails 7 and benefiting from the new features. Turbo and Stimulus allows us to do partial page updates without refreshing the page.
If you are still using Rails 6 or lower, please be aware that with the final release of Rails 7, Rails 6.1 will be placed in "security issues only" mode and will no longer receive bug fixes. This will also be the end of support for Rails 5.2, as it will no longer receive updates.
Are you as excited about Rails 7 as we are? If so, join us as we continue to build great applications with Ruby on Rails. Check out our open positions!
About the author
Asad is a Full Stack Developer based out of Kitchener, ON.