Rails 7.1 - Raises on assignment to readonly attributes

Rails 7.1 adds the raise_on_assign_to_attr_readonly config to config.active_record that is used to raise ActiveRecord::ReadonlyAttributeError error on assignment to attr_readonly attributes. The previous behavior would allow assignment but silently not persist changes to the database. Configuring config.active_record.raise_on_assign_to_attr_readonly In a newly created Rails 7.1 application, config.load_defaults 7.1 is set by default in application.rb. The default value of raise_on_assign_to_attr_readonly for config.load_defaults 7.1 is true and for config.load_defaults < 7.1

A quick dive into the new query_constraints config introduced in Rails 7.1

The query_constraints config introduced in Rails 7.1 is helpful for pre-setting the DB query constraints on the ActiveRecord models.

Rails 7.1 supports infinite ranges for Active Record Validators

Rails 7.1 supports infinite ranges that can be used with Active Record length and inclusivity validators.

Rails 7.1 allows ActiveRecord::QueryMethods#select & #reselect to receive hash values

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 - 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

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 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.

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