Here are links to the other parts of the series:

If you landed here through a search engine or a referral link, this is the second part of a three-part blog post series of features coming to Rails 7.1. Please see the intro section of An Overview Of Rails 7.1 Features. Part I for some context.

I encourage you to scroll down and subscribe to my newsletter for Ruby and Rails goodies if you’d like to stay up to date on developments or tips.

01. Add drop_enum command for Postgres.

Rails had create_enum for Postgres without a matching drop_enum. This pull request added drop_enum while another pull request made drop_enum reversible. The latter pull request made it possible so that if the if_exists: true option is provided, the enum is dropped only if it exists. Otherwise, if the enum doesn’t exist, an ActiveRecord::IrreversibleMigration error is raised.

02. Support multiple preview paths for mailers.

The changes in this pull request make it possible to preview emails from engines, it does so by deprecating config.action_mailer.preview_path for config.action_mailer.preview_paths where one can add locations for mailer previews for Rails to search, so instead of having a single search path set, you can append multiple search paths:

config.action_mailer.preview_paths << "#{Rails.root}/lib/mailer_previews"

03. error_highlight gem to locate columns with errors.

Ruby 3.1 added the error_highlight gem to display the fine-grained location of where an error occurred. Rails will now use error_highlight on error pages to show the column range of where an error occurred.

04. routes --grep to filter routes by paths.

Rails will extend bin/rails routes --grep to match paths too. So if you run $ bin/rails routes -g /cats/1, you can expect to get:

Prefix Verb   URI Pattern         Controller#Action
   cat GET    /cats/:id(.:format) cats#show
       PATCH  /cats/:id(.:format) cats#update
       PUT    /cats/:id(.:format) cats#update
       DELETE /cats/:id(.:format) cats#destroy

05. ActiveRecord::QueryMethods#select accepts a hash.

If you prefer hashes over raw SQL strings, now you can use them with select when you join tables. For instance instead of:

Post.joins(:comments)
    .select(
      "posts.id as post_id, posts.title as post_title,
      comments.id as comment_id, comments.body as comment_body"
    )

You can do:

Post.joins(:comments)
    .select(
      posts: { id: :post_id, title: :post_title },
      comments: { id: :comment_id, body: :comment_body}
    )

06. Pass options accessor to Cache#fetch block.

With this change, it is possible to pass cache options to the fetch method, so if you use third party auth tokens and store them in the cache, you can set the cache expiry time to the same as the expiry time of the token, so the cache can expire along with the TTL of these third party tokens.

Rails.cache.fetch("3rd-party-token") do |name, options|
  token = fetch_token_from_remote
  # set the cache's TTL to match the token's TTL
  options.expires_in = token.expires_in
  token
end

07. Add :locals to Action View rendering instrumentation.

ActiveSupport::Notifications will now capture :locals in addition to :identifier and :layout. This change is motivated by the need to gain additional insights into the rendering process to enable developers to make more accurate assertions about it during testing. Additionally, it may also be useful for APM purposes.

08. Rails.error.report now marks errors as reported.

This is more of a bug fix maybe. Either way, still a great merge in my books. The Rails.error.report method has been updated to mark reported errors as such, to prevent them from being reported multiple times. This is useful in cases where users want to report errors with additional context before allowing them to bubble up, or where errors need to be safely caught and reported outside of the execution context.

09. ActionController::Parameters#exclude? added.

Adding this method keeps things a little more consistent with a Hash. Rails already provides ActionController::Parameters#include? so ActionController::Parameters#exclude? is only natural. The new exclude? method returns true if the given key is not present in the parameters. It’s the inverse of include?.

params = ActionController::Parameters.new(id: 1)
params.exclude?(:id) # => false
params.include?(:id)   # => true

10. Allow passing classes to dom_id.

You can now pass a class directly to dom_id without having to call new. This makes using dom_id more similar to using dom_class, and it also helps to improve performance by avoiding the creation of a new object just to generate a string. This can save a few keystrokes and improve the efficiency of your code.

# Previously
dom_id(Post)
# => NoMethodError: undefined method `to_key' for Post:Class

# Now
dom_id(Post)
# => "new_post"

11. Add the ability to set the tags_format for QueryLogs.

This pull request introduces a new option called tags_format to ActiveRecord::QueryLogs. The tags_format option allows users to customize the separator and quoting style used for key-value pairs in their query logs. By default, tags_format uses a colon (:) as the separator and does not add quotes around values. However, some users may prefer to use a different separator, such as an equals sign (=) as used by the sqlcommenter gem. The tags_format option allows users to choose between the default behaviour and the :sqlcommenter style by setting the option to the corresponding symbol.

12. Facilitate the use of any regular ERB in database.yml.

DummyCompiler has been replaced with ERB::Compiler. True to what dummy compilers do, it’d blow up when one tried to use ERB in a YAML key. This pull request makes it possible to use ERB in YAML keys. While at it, Rails also deprecated config.active_record.suppress_multiple_database_warning.

So we can now do stuff like:

development:
  <% 5.times do |i| %>
  shard_<%= i %>:
    database: db/development_shard_<%= i %>.sqlite3
    adapter: sqlite3
  <% end %>

13. Add column information inside ERB templates.

This pull request adds column information to exceptions that occur inside ERB templates. It builds on the work done in item 3 above. Now we can have some nicety like:

Ruby on Rails error-highlight gem.


That’s it for this instalment! Part III is in the works with about 25 features. What has been your favourite feature so far? You can let me know in the comments.

Stay tuned.

    Powered By ConvertKit
    Consider subscribing to my newsletter for a chance to get my upcoming eBook collection of rad Ruby idioms, tips & tricks or follow me on Twitter to explore Ruby, JavaScript and web technologies.