Rails 7.2 has just been released, and it’s packed with exciting updates for Rails developers.
In this blog, we’ll dive into the key new features and enhancements brought by Rails 7.2.
Railties
-
Default GitHub CI Workflow: Rails now includes a default GitHub CI workflow file in new applications, enabling automated linting, testing, and security checks right from the start.
-
Brakeman Integration: New Rails applications will come with Brakeman preconfigured, ensuring security vulnerabilities are caught early in the development process and combined with Github CI workflow, it will run on every push.
-
Development Containers: Rails 7.2 introduces a default development container setup, making it easier to develop Rails applications in isolated, reproducible environments.
-
Default Rubocop: Rails applications will now come with Rubocop configured by default and it will enforce rules from rubocop-rails-omakase.
-
bin/rails app:update will now support
--force
,--skip
,--pretend
, and--quiet
options. -
Rails will generate default PWA files for manifest and service-worker, which are served from
app/views/pwa
. -
For Rails 7.2 applications
YJIT
is enabled by default if running on Ruby 3.3 or newer.
ActiveRecord
-
Strict Loading Mode Adjustments: In Rails 7.2, strict loading with
:n_plus_one_only
mode no longer eager load child associations. This change aligns with the intended behavior, preventing non-deterministic ordering issues when methods like first or last are called. -
Transaction Callbacks: Rails 7.2 introduces new capabilities for managing transaction callbacks. We can now register callbacks directly within a transaction block, utilize
current_transaction
for attaching callbacks during active transactions, and employafter_all_transactions_commit
to execute code once all transactions are finalized. -
Optimizing Test Performance: Rails 7.2 introduces the
SKIP_TEST_DATABASE_TRUNCATE
flag to optimize test performance on large databases. When all tests run within the default transaction, enabling this flag can skip the database truncation step, significantly reducing the time required for multi-process test runs. -
ActiveRecord::Base#pluck will now accept hash values.
-
BatchEnumerator#destroy_all will now return the total number of affected rows instead of nil.
-
Rails 7.2 introduces ActiveRecord::Base.with_connection to optimize database connections for high concurrency and IO-intensive applications by providing a more controlled approach to manage database connections.
-
Date TypeCasting in PostgreSQL: The PostgreSQL Adapter now decodes columns of type
date
directly toDate
objects instead of strings. -
Rails 7.2 introduces the ability to temporarily ignore counter cache columns until they are backfilled. This feature is especially useful when adding counter caches to existing large tables.
ActionPack
-
Browser Version Control: Rails 7.2 now allows specifying which browser versions can access certain actions. By using the
allow_browser
method, we can block outdated browsers or those that don’t support modern web features. -
Rails 7.2 introduces rate limiting in ActionController, allowing developers to control the frequency of requests to specific actions. By using the
rate_limit
method, we can set limits based on factors like the number of requests within a certain time frame and apply it selectively to actions
ActiveJob
- Preventing Job Scheduling Within Transactions: In Rails 7.2, ActiveJob now automatically defers the enqueuing of jobs until after a transaction is committed. This change prevents potential issues where jobs are picked up and executed before the transaction completes, which could lead to errors. If a transaction is rolled back, the job will be dropped
ActionView
- Rails 7.2 enhances log output by including the count of SQL queries directly in the logs. This update makes it easier for developers to see how many queries are executed during requests, helping to identify performance issues quickly.
ActiveStorage
- Adds support for custom blob keys in ActiveStorage.
ActiveSupport
- Adds ErrorReporter#unexpected to report precondition violations.