Rails 7 - Associations across databases with disable_joins

Rails 7 allows to fetch data from multiple databases for a has_many/has_one:through association using the disabled_joins option.

Rails 7 adds new options to upsert_all

We often come across use cases in a Rails application where we need to create, update, or delete records in bulk. Most ORMs naively update or insert each record individually by executing N+1 database queries. To address this problem, Rails 6 added the upsert_all method that provides a faster way to update and insert a large number of records. upsert_all uses a single SQL INSERT statement for updating/inserting the records. And it does so without instantiating any models. Nor does it trigger any Active Record callbacks or validations ! How upsert_all works? If we check the

Rails 7 introduces ComparisonValidator to compare data with validation

Rails 7 adds ComparisonValidator to compare and validate numeric, date and string values. It provides comparisons options that accept value, procs, or symbol.

Rails 7 brings Import Maps into the limelight

Rails 7 puts forward an alternative approach to bundling JS called Import Maps. Read this blog to find out more about it.

Deep dive into ActiveRecord .scoping method in Rails

In Rails, multiple scopes can be created and chained together. What if we wish to apply a specific scope to a group of queries? Consider the following scenario: we have Post and Comment models and we want to perform few operations on public posts. # app/models/post.rb class Post < ActiveRecord::Base scope :public, -> { where(private: false) } endLoading development environment (Rails 7.0.0.alpha2) 3.0.0 :001 > Post.public.update_all(body: 'public post') Post Update All (4.1ms) UPDATE "posts" SET "body" = ? WHERE "posts"."private" = ? [["body", "public post"], ["private", 0]] 3.0.0

Rails 7 introduces partial_inserts config for ActiveRecord

Rails 7 adds the partial_inserts config to config.active_record that is used to control partial writes when creating a new record. config.active_record.partial_inserts is a boolean value and controls whether or not partial writes are used when creating new records (i.e. whether inserts only set attributes that are different from the default). Configuring config.active_record.partial_inserts In a newly created Rails 7.0 application, config.load_defaults 7.0 is set by default in application.rb. The default value of partial_inserts for config.load_defaults 7.0 is false and for

Rails 7 supports tracking of belongs_to association

One of the most convenient features of Rails is the ability to track attribute changes. Rails 7 is releasing soon and bringing in a lot of new features as a treat for developers. One of the many features that Rails 7 is introducing, is that we can also track the associated object. This Rails ActiveRecord specific PR has added two methods for tracking the changes for the belongs_to association. In this article, we will discuss these two new methods with the help of examples. 1. association_changed?The association_changed? method tells if a different associated object has been

Side effects of Active Record's new feature #invert_where in Rails 7

Rails 7 is introducing a new method invert_where that will invert all scope conditions. It allows us to invert an entire where clause instead of manually applying conditions. We can either chain invert_where to a scope or to a where condition. class Account scope :active, -> { where(active: true) } end Account.active.invert_where => "SELECT \"accounts\".* FROM \"accounts\" WHERE \"accounts\".\"active\" != 1"Account.where(active: true).invert_where => "SELECT \"accounts\".* FROM \"accounts\" WHERE \"accounts\".\"active\" != 1"What are the various side effects of using invert_where?1. The invert_where method inverts all the where

Active Record Encryption in Rails 7

If you're hosting your web server in a particular region, it might be necessary to comply with the GDPR norms of that region. Anonymizing and encrypting data becomes necessary in such situations. In this blog, we will discuss the attribute encryption that Rails 7 provides right out of the box. And we will also see the Deterministic & Non Deterministic approaches. (If you're using Rails version lesser than 7, check out our previous blog here on how to write a custom encryption framework.) ActiveRecord attribute encryption in Ruby on Rails for better securityIn this blog post, we will explore a

Speeding up Rails 7's Controller Actions using ActiveRecord's #load_async

Most of the time in a web application, a single API request consists of multiple database queries. For example: class DashboardsController < ApplicationController def dashboard @users = User.some_complex_scope.load_async @products = Product.some_complex_scope.load_async @library_files = LibraryFile.some_complex_scope.load_async end end Quoting snippet from load_async PR description from rails repository. The queries are executed synchronously, which mostly isn’t a huge concern. But, as the database grows larger in size, the response time of requests is getting longer. A significant part of the query time is often just I/O waits.