ActiveRecord
Unboxing Rails 7: What's new in the latest major version

Summarized using AI

Unboxing Rails 7: What's new in the latest major version

Claudio Baccigalupo • May 17, 2022 • Portland, OR

The video titled "Unboxing Rails 7: What's new in the latest major version" presented by Claudio Baccigalupo at RailsConf 2022 provides an in-depth overview of the significant changes and features introduced in Rails 7. The presentation emphasizes that Rails 7 was released in December 2021 and marks a substantial evolution in the framework, building upon over 4,000 commits made by 500 contributors. Baccigalupo's talk is structured to explore changes in key libraries such as Ruby, Active Support, Active Record, and more. Here are the main points covered in the video:

  • Compatibility with Ruby: Rails 7 requires Ruby 2.7 or higher, recommending Ruby 3 for better performance.
  • Active Support Enhancements: New methods like sole, max, and min have been added to facilitate more efficient data operations.
  • Active Model Validations: New validation options, including passing a range to validate_numericality and a powerful comparison validator, enhance model validations.
  • Active Storage Updates: Active Storage now supports audio file analysis, expiring URLs, and can integrate with cloud services seamlessly.
  • Action View Helpers: Improvements to HTML rendering methods such as link_to, phone_to, and sms_to make it easier to create and manage links.
  • Active Record Improvements: Notable features include encrypted attributes, asynchronous query loading with load_async, excluding method for querying, and an invert_where method.
  • Rails 7 Simplification: Rails 7 removes the default inclusion of Spring and Listen for better performance management during development.
  • JavaScript and CSS Bundling: Rails 7 transitions away from Webpacker, allowing users to select their preferred JS bundler (like Rollup or ESBuild) upon app creation, and introduces the CSS bundling to enhance flexibility.
  • Introduction of Import Maps: By adopting import maps, Rails 7 offers a bundling-free front-end development option, defaulting to Hotwire, which enables modern JavaScript without requiring a bundler like Webpack.

In conclusion, Baccigalupo encourages developers to upgrade to Rails 7 and highlights the importance of community contributions to the framework. Through these advancements, Rails 7 aims to improve developer experience, enhance performance, and expand the framework's versatility in modern web development.

Unboxing Rails 7: What's new in the latest major version
Claudio Baccigalupo • May 17, 2022 • Portland, OR

Rails 7.0 removes webpacker and spring from the default stack, adds encrypted attributes, allows for asynchronous query loading, changes autoloading defaults, attaches comments to Active Record queries, and introduces new tools for front-end development.

Learn about these and many other Pull Requests that were merged in rails/rails in 2021. Understand the motivation behind some architectural decisions. Review the process to upgrade from Rails 6.1 to Rails 7.

RailsConf 2022

00:00:00.900 Hello everyone! As everyone on this stage has said, it's awesome to be back on stage.
00:00:03.800 It feels like a long time since we were last here, and maybe not a lot has happened in the last two years. However, we've already seen through the keynotes that a lot has been going on in Rails. Most importantly, we had a new major release of Rails last December. Rails 7 is now out!
00:00:22.859 You might have seen blog posts and tutorial videos about this release, particularly those that focus on changes to Active Record and Rails ties. While these changes are important, there have been many other changes that deserve attention.
00:00:36.780 This is why I wanted to give this talk. I want to highlight features that you may not be aware of. Even if you leave this talk with just one cool piece of information about Rails 7, I hope it encourages you to update. I think that would be great!
00:01:05.460 Also, I won't be able to discuss everything new in Rails 7, as it's been 12 months of development with more than 4,000 commits by 500 people. But I definitely want to acknowledge the contributions from these individuals, particularly the top 60 contributors to this version of Rails. If you see any of their names around, please go and thank them for their amazing work on this framework.
00:01:28.740 So, regarding the structure of this talk, I will go through changes library by library to give you an overview of what has changed. I'll start with Ruby and then cover Active Support, Active Model, Active Storage, Action View, Active Record, and Rails ties. For each change, I'll provide a PR number or commit number so that you can explore further if you're interested.
00:02:07.259 First off, not only has Rails evolved, but Ruby itself has also seen significant updates. There's a new release every Christmas, so if you want to use Rails 7, you need at least Ruby 2.7, although Ruby 3 is recommended. This shouldn't be a big deal; these versions are backward compatible and have been out for a couple of years. Additionally, they are more performant, which will benefit your applications.
00:02:44.419 Ruby 2.7 introduced a new method called filter_map, which some contributors have already integrated into the Rails codebase to replace map.compact with this new, more performant method. Just like Aileen talked about earlier, contributing to Rails can be something that any one of you can do.
00:03:13.440 Additionally, there's a new debugger included by default in Rails. For the last few years, the default debugger was called Byebug, but now we are saying goodbye to Byebug and welcoming a new debugger simply called Debug. The syntax for this debugger is quite similar to other debuggers, but it is more compatible with Zeitwerk, as mentioned in the keynote.
00:03:41.280 Now let’s talk about Active Support. This library extends Ruby by defining new methods on Ruby classes. For instance, in Rails, you can use .to_time, which is not available in Ruby itself. One new method added to Enumerable is called #sole. It retrieves the first item in a collection, provided there is only one item.
00:04:08.280 If you try to invoke this method on an empty set or a hash with more than one item, you'll receive an exception. The nice thing about this method is that it can also be used within Active Record query methods, so you can write something like 'give me the sole user where the admin flag is true.' This feature allows you to enforce a strict requirement that only one user should match your query; otherwise, it raises an exception. Other methods added to Enumerable include #maximum and #minimum, making it simple to retrieve the highest or lowest values within an array of objects.
00:05:40.740 Next up is Active Model, the Rails library that deals with attributes, errors, and validations. One existing validation method is validate_numericality, which checks if an attribute is a number. With Rails 7, you can now pass a range to this validation, checking if an attribute falls within a specified range, such as determining if an age is between 18 and 99.
00:06:11.460 Additionally, there's a new and powerful validation called comparison validator. For instance, if you have a form that asks for the user's birth date, you may want to ensure that their birth date is less than or equal to today. Otherwise, they would be born in the future, leading to a time travel paradox!
00:06:47.460 There are other neat validations where you can ask for a preferred name, which could be left blank. However, if provided, it should differ from their actual name. This flexibility in validation is a powerful new addition in Rails 7.
00:07:18.480 Now let's move on to Active Storage, which deals with file attachments, uploads, and downloads. One great feature of Active Storage is that it has analyzers. If a user uploads an image, Active Storage can determine dimensions, resolution, and whether it is black and white, and this extends to videos as well.
00:07:51.180 With Rails 7, this functionality has expanded to audio files. Now, if your users upload audio files, Active Storage can provide information such as duration and bit rate. Another significant improvement is that Active Storage now integrates seamlessly with cloud services like Amazon S3 and Google Cloud.
00:08:33.120 These cloud services have a feature called expiring URLs. For example, if you want to point to an image on S3 but want the link to expire after a specific time (say 60 minutes), you can easily do this in Active Storage. When using the blob path method, you can specify an expiration time, setting the URL to expire after your desired duration.
00:09:01.080 Action View is the library responsible for rendering ERB files and producing HTML. It contains many helpers, and probably the most widely used one is link_to, which generates an HTML link for your website. Several enhancements have been made to link_to, as well as other methods like phone_to and sms_to.
00:09:52.200 In Rails 7, the second argument for methods like phone_to and sms_to is no longer strictly required. This means if you specify only the phone number, Rails will automatically use that number as a display text as well. For link_to, if you provide a conventional resource, Rails will intelligently use that resource's name as the link caption.
00:10:43.140 I want to check how many people have already used Rails 7. So far, it seems around 30 of you have!
00:11:15.300 We're halfway through our journey! I also want to mention that there are changes to Active Job and Action Text, but I’ll summarize those for you.
00:11:57.300 Now, let's talk about one of the coolest features, Active Record. A key addition introduced in this release is encryption for Active Record. When using Active Record, your models (e.g., a Person model) typically have attributes stored in plain text within a database table. However, in Rails 7, you can now specify which attributes should be encrypted.
00:12:44.280 This is a significant enhancement, as it allows you to avoid storing sensitive information in plain text. The method is user-friendly, allowing Rails developers to simply call this method without needing to manually handle encryption underneath. It's a remarkable addition to Rails.
00:13:10.620 Another exciting addition to Active Record is the load_async method. Imagine you need to display categories and posts on an index page, and each query can take time to respond. By using load_async, you can request the categories without blocking the page rendering, meaning the user doesn't have to wait for all requests to complete.
00:13:41.460 Instead, the queries run in the background and do not block rendering, making the application feel responsive. This is especially beneficial for handling slow queries or scopes that you don't need immediately.
00:14:07.920 In addition, a new method called excluding (or its alias without) allows you to load posts while excluding specific ones, simplifying the code you would generally need if you were to filter out redundant posts.
00:14:30.240 Lastly, Rails has introduced a new method called invert_where. This method can simplify your logic when retrieving opposing conditions for certain user scopes.
00:15:04.020 If you're interested in more details on these changes, please check out the Rails changelog files, which contain comprehensive records of updates.
00:15:55.920 Next, let's talk about Rails ties, the library that puts all the pieces together. When you create a new Rails 7 application, two gems will no longer be included by default: Spring and Listen. While these gems were designed to enhance performance during development, their relevance has decreased with advancements in technology.
00:16:26.640 While they can still be uncommented and used, this change reflects the broader trend toward optimizing developer experience without excessive overhead.
00:16:56.240 This leads us to the prominent PR that has garnered attention for replacing Webpacker. Rails 7 acknowledges the rapidly evolving JavaScript landscape. Instead of being limited to Webpack, developers now have the flexibility to choose their preferred JavaScript bundling strategy.
00:17:30.920 You can specify your preferences when creating a new app, allowing you to use ESBuild, Rollup, or even stick to Webpack. This liberates developers and matches the current front-end ecosystem.
00:18:05.280 Similar changes have been implemented in CSS. Rails now supports new conventions, giving you the option to select your preferred CSS framework instead of being restricted to one.
00:18:44.000 With this flexibility, you can have a cleaner setup that caters to your specific needs without the added burden of unnecessary dependencies.
00:19:25.560 As we wrap up, I want to mention one last set of changes regarding JavaScript. Yesterday, it was noted that browsers now support import maps—a significant shift in how front-end development can occur without bundling.
00:19:56.700 This development allows Rails users to write JavaScript directly for the browser without the previous need for build steps. This change invites all Rails developers to consider a new way of structuring their JavaScript code.
00:20:40.500 In summary, Rails 7 is not just a collection of updates but a significant evolution in how we approach application development. This includes paving the way for modern front-end frameworks while providing developers the freedom to structure their applications with evolving technology.
00:21:25.800 Thank you for your time today! I hope everyone will take the plunge and update to Rails 7 if they haven't already. I encourage you to explore the contributions you can make to this amazing framework—it's a rewarding experience!
Explore all talks recorded at RailsConf 2022
+64