Rails 7.1

Summarized using AI

Powerful Rails Features You Might Not Know

Chris Oliver • October 16, 2023 • Amsterdam, Netherlands

In the video titled "Powerful Rails Features You Might Not Know" presented by Chris Oliver at Rails World 2023, the speaker delves into a variety of powerful yet often overlooked features of the Rails framework, particularly focusing on updates and enhancements introduced in Rails 7 and 7.1. With a runtime of approximately 35 minutes, Oliver emphasizes the importance of familiarizing oneself with the latest capabilities of Rails to improve development efficiency and solve problems more effectively.

Key Points Discussed:

  • ActiveRecord Enhancements:

    • Introduced an 'excluding' method to simplify querying users by excluding those who opted out of notifications.
    • Added 'strict_loading' which helps avoid N+1 query problems by enforcing the loading of associated records.
    • Features like 'attrreadonly' that protect specific attributes from being changed and 'withoptions' that reduce redundancy in method calls.
  • ActiveStorage and ActionText Features:

    • Named variants in ActiveStorage for better image management.
    • ActionText allows embedding Active Record models in rich text, enhancing dynamic interactions in applications.
  • Innovations in Turbo and Hotwire:

    • Discussed custom Turbo Stream actions that enhance real-time user interactions and notifications during long-running processes.
  • ActiveSupport Utilities:

    • Features like 'truncate_words' for better text handling and various time helper methods that enhance productivity in time-related queries.
  • Updates in Rails 7.1:

    • New features such as 'local environment checking', 'unused resources routes', 'template strict locals', and improved authentication capabilities with hassecurepassword.
    • Enhanced Active Storage for easier image variant management and streamlined processes for generating secure authentication tokens.

Significant Examples:

Oliver illustrates the usage of the 'excluding' method through a real-world scenario where notifications to users need to be sent while excluding certain individuals. This shows how the method abstracts SQL complexities, allowing developers to focus on requirements rather than low-level database queries.

Additionally, he discusses the ability to embed Active Record models in rich text through ActionText, enhancing the way users interact with text content in modern Rails applications.

Conclusion and Takeaways:

Oliver concludes by encouraging developers to continuously learn and stay updated with the latest features in Rails, as this knowledge can significantly boost productivity and make coding experiences smoother. He emphasizes that understanding and utilizing these features can contribute substantially to building more efficient and maintainable web applications.

Powerful Rails Features You Might Not Know
Chris Oliver • October 16, 2023 • Amsterdam, Netherlands

An unbelievable amount of features are packed into Rails making it one of the most powerful web frameworks you can use. @GorailsTV creator Chris Oliver takes a look at some little known, underused, and new things in Rails 7.

A few of the topics covered:
- ActiveRecord features like “excluding”, strict_loading, virtual columns, with_options, attr_readonly, etc
- ActiveStorage named variants
- ActionText embedding any database record
- Custom Turbo Stream Actions with Hotwire
- Turbo Native authentication with your Rails backend
- ActiveSupport features like truncate_words
- Rails 7.1’s new features: authentication, normalizes, logging background enqueue callers, and more.

Links:
https://rubyonrails.org/
https://rubyonrails.org/2023/10/5/Rails-7-1-0-has-been-released

#RailsWorld #RubyonRails #Rails7 #opensource #OSS #Rails #ActiveRecord #ActiveStorage #ActiveSupport

Rails World 2023

00:00:15.920 Hello everybody! How are you doing? We are going to talk about a whole bunch of topics in the next 35 minutes. This time, not about TypeScript, but the goal of this talk is to remind you to pay attention to everything that's available to you.
00:00:22.920 You know, when all you have is a hammer, everything looks like a nail. It's pretty easy to solve problems in the same way you've always done it. However, there's a lot of new stuff always coming out in Rails, so we will cover various features, some of which you might already know and others you might not.
00:00:30.240 We'll also touch on a lot of the new features in Rails 7. Let's start off with a simple example that demonstrates this.
00:00:36.960 When you want to send notifications to users, like when you're in a GitHub issue or pull request, you need to send email notifications out to all the users in the thread when a new comment shows up. However, we need to skip the people who opted out of notifications and we don't want to send it to the user who created that comment.
00:00:50.399 This is fine, but we have to drop down into SQL. We need to write a query that gives us all the users whose IDs are not in this list of users who opted out or the people we want to skip.
00:01:02.239 This leads us to thinking about the problem from a SQL level. However, Active Record has a new 'excluding' method added in Rails 7, which allows us to say instead, 'give me all the users excluding these users.'
00:01:13.840 Instead of thinking about the problem in SQL terms, we are now thinking about it in terms of requirements, allowing us to grasp it much quicker.
00:01:20.360 Another thing we can do is remove dependencies. If you've ever used the Bullet gem to detect N+1 queries, it's just another dependency to maintain and update.
00:01:28.720 Active Record has added 'strict loading'. This allows you to add this feature either as a scope on your queries or on associations.
00:01:35.759 When we load the first project without saying anything about loading the comments, it will raise an error telling us that there is an Active Record strict loading violation error. This means lazy loading is not allowed for that association.
00:01:50.360 To do this correctly, we want to explicitly state that when strict loading is enabled, we say, 'project includes comments,' and then grab the first project. This will load the project along with the comments into memory and ready for use.
00:02:06.760 Now, whenever we ask for 'project.comments', it will work without any issues. This allows us to decide precisely when we want to use strict loading.
00:02:15.599 Another cool feature of Rails is generated columns support. This is currently not added to SQLite yet, and there are still rough edges on Postgres.
00:02:20.720 However, it has support in MySQL, and in Rails 7, it allows you to combine data at the database level.
00:02:30.600 For example, if we're using separate first and last name columns in the database but want to query on the full name, we can compute that at the database level. This means anytime we insert or update a record, it will automatically recompute the full name.
00:02:42.760 If you decide not to store it and instead calculate it when querying, it can be useful for other things, like dollars and cents or temperatures in Celsius and Fahrenheit.
00:02:54.560 Another valuable feature is 'attr_readonly', which allows you to mark an attribute as read-only. For example, if you have a super admin flag that you don't want users to change, you can use attr_readonly.
00:03:06.520 This will ignore changes anytime you try to update it. Even if someone tries to circumvent it using update_column, it will skip that as well.
00:03:20.560 'with_options' is a feature in Active Support that reduces redundancy when adding similar parameters. For example, when we call with_options, it will automatically include specified options such as dependent: destroy.
00:03:30.480 This isn't limited to Active Record; it can be used in internationalization as well. This simplifies method calls for redundant tasks.
00:03:48.240 Next, we have 'try', which is useful for calling methods conditionally. Instead of checking if an object responds to a method, you can use 'try' to call the method safely.
00:03:58.720 This improves readability and eliminates complex fallback logic when dealing with method calling.
00:04:08.240 Another super useful feature of Rails is Action Text. At first glance, it appears as a rich text editor that allows you to format text. However, you can also embed any Active Record model within your text.
00:04:19.559 For example, when you type a user's name or mention a commit in GitHub, it embeds the relevant database record directly into the text.
00:04:30.639 This means that anytime you update your avatar or name, next time it renders, it shows the latest version, which is quite cool.
00:04:45.760 When typing a user to mention, you would get some data back from the server in JSON format, including an attachment SGID.
00:04:52.720 This SGID is what you use to embed the record in the editor. When saving to the database, you are actually not saving the HTML for that entry; just an Action Text attachment with that SGID.
00:05:03.920 Rails will then look up the user, render the partial for it, and display the final result as plain HTML.
00:05:21.760 Action Text allows you to reference any model in your app in an intuitive way by using mentions or hashtags.
00:05:34.640 In Action Text, you even have the serialization capabilities that allow you to save non-Ruby hashes into other objects, letting you customize how data is stored and accessed.
00:05:44.560 For the sake of demonstrating how you can create objects through Active Record, you can use the load and dump methods effectively.
00:05:58.720 Active Mailbox allows inbound emails to be processed efficiently. For instance, you might find it convenient to allow users to reply to comments via email.
00:06:08.720 The way this works is by defining your mailbox routes with a regex to match the sender's email address and process the received email seamlessly.
00:06:22.320 By routing emails through designated mailboxes, you can look up conversations in your database and create new posts from the email content, enhancing user experience.
00:06:35.480 Incorporating features like In-Reply-To and References can assist in threading the conversations together for emails.
00:06:42.920 Another useful feature that might not get enough attention is routing constraints. When building applications, you often want to separate marketing pages from app functionality.
00:06:57.120 For instance, if you're on a subdomain, you can render different views based on whether users are on the application part or the marketing site.
00:07:05.960 You can also check if a user is logged in or is an admin before showing the specific routes based on the user's role.
00:07:20.440 The feature of drawing routes from other files helps keep your routing files organized, making it easier to manage when building large applications.
00:07:35.120 You can create generators within Rails for various purposes, such as API clients. These generators can set up default settings and help manage different API integrations easily.
00:07:49.440 Custom Turbo Stream actions allow for real-time updates, such as notifying users about the status of long-running imports or changes within your application.
00:08:02.920 By utilizing the Browser Notification API, you can keep the users informed about these changes intelligently. This enriches the interaction and engagement.
00:08:16.120 There are many Active Support features that can significantly enhance productivity. For instance, instead of using a plain truncate, you can use truncate_words to avoid cutting off words unexpectedly.
00:08:27.612 There are also a bunch of time helpers available that make it easier to manage and display time-related data succinctly.
00:08:41.760 With methods like all_day, all_week, and all_month, you can simplify your time range queries significantly.
00:08:55.760 Additionally, there's a helpful number helper, number_to_human, to format numbers into human-readable forms eliminating the need to write custom formatting logic.
00:09:09.120 As a closing note, let's discuss some new features introduced in Rails 7.1. Rails now supports a super cool ability to check the environment locally with rails M local.
00:09:23.920 This reduces the repetition of checking for certain environments, making your code cleaner.
00:09:39.240 For instance, you can now use unused resources routes, which allows you to identify any unnecessary routes, ensuring that only necessary ones are available.
00:09:55.240 Template strict locals also comes in handy, making sure that required locals for your render calls are enforced, leading to better error handling.
00:10:08.440 The normalizes method simplifies input handling by ensuring values are trimmed and formatted properly, avoiding the necessity for extra conditions in your code.
00:10:20.920 Improvements to has_secure_password allow for better handling of authentication. With the new methods introduced, it helps enhance security against timing attacks.
00:10:34.280 Lastly, the generate token feature allows you to create secure tokens for actions like password resets, which have built-in expiration features.
00:10:48.440 Active Storage's recent updates make managing image variants and previews straightforward, enhancing usability and performance.
00:11:00.160 This is everything for today. Always be learning and keeping up with the latest features helps improve productivity! Thank you!
Explore all talks recorded at Rails World 2023
+25