Rails 7.1 - Optimizes Active Record batching for whole table iterations

In Rails, Active Record provides batch processing for ActiveRecord::Relation with the in_batches method. In Rails 7.1 the implementation of in_batches has improved to give optimized results for whole table iterations. In this article, we will see how it has improved. Example User.in_batches(of: 3) do |relation| puts relation.to_sql end Before Rails 7.1 Loading development environment (Rails 7.0.4) 3.0.0 :001 > User.in_batches(of: 3) do |relation| 3.0.0 :002 > puts relation.to_sql 3.0.0 :003 > end User Pluck (0.4ms) SELECT

Rails 7.1 adds ActiveRecord::Base::generates_token_for API

Generating special tokens that are unique, tamper proof and that can store information like the purpose of the token and the token's expiry can be very useful in certain scenarios. You can create a unique token for specific purposes like email_verification or password_reset, attach them to your application URL endpoint and send it to the user via email. Up until now, you might have used the ActiveRecord::SignedId API that allows you to create expirable tokens. And you can query ActiveRecord to find the record using the signed id. Consider a scenario where we want to send a

Autoloading pitfalls fixed by Rails 7’s default Zeitwerk mode

Rails 7 onward zeitwerk mode completely replaces classic autoloader thereby resolving the flaws of classic autoloader

Rails 7.1 - construct Common Table Expression using .with query method

In Rails 7.1 .with query method makes it super easy to build and chain complex Common Table Expression CTE queries.

Rails 7 adds in_order_of for ActiveRecord::QueryMethods and Enumerable

Quite often in our Rails application, we need to retrieve records sorted in a specific order. For example, in an e-commerce application, we want to retrieve the customer orders in the order of "Completed, Cancelled, In Transit, Pending" statuses. The way to do it is to write an explicit SQL query. Starting Rails 7, this can be achieved simply with the help of the in_order_of method for ActiveRecord::QueryMethods and Enumerable. This article explains how we can use the in_order_of method to sort data in Rails 7. 1. ActiveRecord::QueryMethods#in_order_ofConsider we have a

Rails 7.1 introduces destroy_association_async_batch_size

Rails 7.1 now allows you to tweak the destroy_async behavior by allowing you to destroy the dependent associations in multiple batches.

Rails 7.1 adds authenticate_by when using has_secure_password

Rails 7.1 introduces a method authenticate_by, used with has_secure_password to prevent timing-based enumeration attacks.

Turbo - The heart of Hotwire

Turbo provides the most basic building blocks for building Hotwire application. In this article, let's learn more about its features.

Advanced features provided by the new debug gem in Ruby

debug is Ruby's new default debugger included in Ruby 3.1. This new debugger has replaced byebug in Rails 7. Not only does debug provide us with a wide range of functionality, but it also provides some advanced features. In this article, we will explore and understand a few advanced features of debug. 1. Seamless integration with VSCode Below steps can be followed to integrate debug gem in VSCode. Install extension VSCode rdbg Ruby Debugger - Visual Studio Marketplace in your VSCode.Open the file you want to debug in the VSCode.Register the breakpoint by clicking on the line

Rails 7 auto detects inverse_of for scoped associations

Rails framework is famous for developers' happiness and making things simpler due to its magic, provided developers follow proper conventions. To extend this magic and to make things simple further, Rails 7 has introduced a change with this PR after which, inverse_of would be inferred automatically for model associations having scopes. In this article, we'll dive into understanding it with examples. Let's say we have a Project model with many assigned tasks. # app/models/project.rb class Project < ActiveRecord::Base has_many :tasks, -> { assigned } end # app/models/task.rb class Task < ActiveRecord::Base belongs_to