An ActiveRecord query by default uses the SELECT * operator to select all the fields from the result set. The ActiveRecord::QueryMethods#select method allows you to select a subset of fields from the result set. Before Rails 7.1 ActiveRecord::QueryMethods#select and ActiveRecord::QueryMethods#reselect only accept strings, symbols, or raw SQL to define columns and aliases to select. When querying a single model, we can provide column names as an array of strings or symbols, or we can provide raw SQL. > Model.select(:field, :other_field) > # OR > Model.select('field', 'other_field') > # OR >
Rails 7.1 adds the --parent option using which we can create a job that inherits from the superclass. Let's learn how to use it.
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
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
Rails 7 onward zeitwerk mode completely replaces classic autoloader thereby resolving the flaws of classic autoloader
In Rails 7.1 .with query method makes it super easy to build and chain complex Common Table Expression CTE queries.
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 now allows you to tweak the destroy_async behavior by allowing you to destroy the dependent associations in multiple batches.
Rails 7.1 introduces a method authenticate_by, used with has_secure_password to prevent timing-based enumeration attacks.
Turbo provides the most basic building blocks for building Hotwire application. In this article, let's learn more about its features.