The Rails Changelog

Rails 7.1 will add several enhancements to existing functionalities, these include enhancements on destroying records in a background job and preparation to support composite primary keys.

Please see extended show notes for details.

Creators & Guests

Host
Emmanuel Hayford
Ruby, Rails. Boxing, chess. Podcasting @railschangelog. Rad Ruby @radrubydev. ⚡️ Rails tips weekly. This Week In Rails co-editor.

What is The Rails Changelog?

Stay in the loop with development news around Ruby on Rails and Ruby. Hosted by Emmanuel Hayford.

[00:00:00] Welcome to the first episode of the Rails Changelog. Now, before we get on with the show, I'd like to inform you of a few things. The first is that like most podcasts, this one is also going to have a show notes page, and the link to the show notes page is notes.railschangelog.com. This link redirects my personal blog.

[00:00:28] The reason is so that I'll be able to have more control over how I format code and how I share screenshots. And probably the most important thing that you should know about is the seemingly random numbers that I'll be calling out throughout these episodes. These are going to be pull request numbers that you can look up if you go to github.com/rails/rails/pull/#. And if you have these [00:01:00] numbers, if any of these pull requests, pique your interest, you can just go into your browser and then find out more about all of these pull requests. You should also probably know that all of these episodes are going to have transcripts just in case my accent is one that you're not used to.

[00:01:15] I'll try to share the transcripts on YouTube or inside of the show notes that you'll see on the show notes page. With all of these out of the way, let's get onto the first item! ⚡️ You are listening to, listening to the Rails Changelog ⚡️. Now, if you remember, almost three years ago, Rails introduced a new association option.

[00:01:42] The association option in question was `destroy_async`. Just like the name of the option implies this option destroyed dependent records in a background job. Rail 7.1 is going to enhance this feature in ways that make lots of [00:02:00] sense for applications that have huge numbers of records to destroy in the background.

[00:02:04] And this feature in Rails 7.1 comes by way of PR number 44617. The way this feature enhancement is going to work is through a configuration option, and the name of the option is

[00:02:17] active_record.destroy_association_async_batch_size

[00:02:24] so how's it going to play out? In Rail 7.1?

[00:02:27] The idea is that you would set a batch size inside your configs file, and the but size would be say, 1000. What would then happen if you asked Rails to destroy a parent record that had 3000 dependent records? In our case, because we have 1000 set as the but size rails would then divide the destruction process among three separate jobs.

[00:02:53] So each job would be destroying 1000 records. On the other hand, if you had a lesser number of dependent records than you [00:03:00] have set in your configs file is going to go ahead and follow the default behavior of `destroy_async`, which is to destroy all of these records in a single background job.

[00:03:11] The next feature is one that a few people didn't like. I particularly like it because it's convenient for the stuff that I do, PR 46786 introduced a single predicate method that you could use in place of two predicate methods. With Rails you can do `rails.env.development?`, which returns `true` if the Rails environment is a development one.

[00:03:31] Likewise, you can do `rails.env.test?`, which returns `true` if the environment is a test environment. Now there's this predicate method that you can use in place of these two methods, so you can do `rails.env.local?`, which returns true if the environment is a development one or if the environment is a test environment.

[00:03:50] I posted this on Twitter and received a lot of mixed responses. We have Twitter user, William saying, and I quote, not a good change in my [00:04:00] opinion. 'env' needs to be succinct. On top of that, you only ever use this line less than a handful of times. On the other hand, we have Celso saying quote, perfect! Unquote.

[00:04:11] 47720 is a PR that enhances the ID method to accept an array of numbers that are going to represent some composite primary keys. The way you would use this is if you had a book model, you could do Book.new.id is equal to an array of numbers. These numbers representing a set of composite primary keys.

[00:04:34] Then if you called the ID method on this book model, you would have the of these composite primary case returned to you. Apparently Rails doesn't support composite primary keys. This PR and several others are an effort to help Rails reach the goal of fully supporting composite primary keys. However, if at this very moment you want to extend.

[00:04:59] Active record to [00:05:00] support composite primary keys. There is a gem called composite_primary_keys that you can leverage to make use of composite primary keys in your application. In other news, around Ruby and Rails, there is RailsConf happening in the States, this year between April 24th and April 26th, if you're planning on going, I think this might be the best time to get yourself a ticket, otherwise you risk missing out.

[00:05:28] Then there is RubyKaigi, which is happening between May 11th and May 13th in Japan. So probably around this time is a good time to start. For RubyKaigi 2023. If Japan happens to be too far from you and you live in Europe, the Ruby user group Berlin is looking for talks for the month of April. So if you are looking to still get involved with the Ruby community, somehow I think this is a good opportunity to present a [00:06:00] talk.

[00:06:00] Or even just attend. Last, but not the least. If you want to stay up to date with Ruby and Rails development news, I would encourage you to subscribe to This Week In Rails. A link to which I'll leave in the show notes. And that brings us to the end of this episode of The Rails Changelog.