Friday, June 9, 2023

This Week In Rails - June 9, 2023

Posted by Emmanuel Hayford

Hi! Emmanuel here writing from a train to Katowice! There have been quite a few developments in the Rails codebase over the last few weeks! Let’s take a look at some of them, shall we?

Create a class level #with_routing helper The with_routing helper can now be called at the class level. When called at this level, the routes will be set up before each test and reset after every test. For example:

class RoutingTest < ActionController::TestCase
      with_routing do |routes|
        routes.draw do
          resources :articles
          resources :authors
      def test_articles_route
        assert_routing("/articles", controller: "articles", action: "index")
       def test_authors_route
        assert_routing("/authors", controller: "authors", action: "index")

Allow composite primary key to be derived from schema When launching an application with a schema that incorporates composite primary keys, there will no longer be a warning issued, and the ActiveRecord::Base#primary_key value will not be set to nil anymore.

For instance, consider a table named travel_routes and a corresponding TravelRoute model. In this case, the TravelRoute.primary_key value will be automatically derived as [“origin”, “destination”], as demonstrated in the following example:

create_table :travel_routes, primary_key: [:origin, :destination], force: true do |t|
     t.string :origin
     t.string :destination
   class TravelRoute < ActiveRecord::Base; end

Store connection_pool in database-related exceptions When exceptions are raised from an adapter, it is beneficial to include the connection_pool as it offers additional context. This context includes information about the connection that triggered the exception, as well as details regarding the role and shard involved.

Add engine draw_paths to app By adding the engine’s draw paths to the application route set, the application gains the capability to access and utilize route files defined within engine paths.

Improve quoted parameters in mime types The Mime::Type now offers support for handling types with parameters, ensuring proper handling of quotes. During the parsing of the accept header, the parameters preceding the ‘q’ parameter are retained, and if a matching mime-type is found, it is utilized accordingly. To maintain the existing functionality, a fallback mechanism has been implemented to search for the media-type without the parameters.

Support batching using composite primary keys and multiple column ordering The find_each, find_in_batches, and in_batches methods now offer support for multiple column ordering. When these methods are used on a table with composite primary keys, it is possible to specify ascending or descending order for each individual key. Example:

Person.find_each(order: [:desc, :asc]) do |person|

That’s it for today. In the past seven days, we had the privilege of witnessing the contributions of 30 amazing individuals to the Rails framework!

Talk to you in the next one!

Your weekly inside scoop of interesting commits, pull requests and more from Rails.

Subscribe to get these updates mailed to you.