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

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.

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.

Speed up time-sensitive database process in Rails applications using a Connection pool, Threads, and Arel

Sometimes dealing with DB processes in a Rails application consume a lot of time. Often there are a bunch of small and simple DB queries that go endless. For example, bulk insertions or bulk updates in tables. Dividing it into various background jobs is an idle solution. In a few scenarios, we can't prefer background jobs e.g. the change you apply, makes the existing data invalid. In this article, we will see how to complete a large set of DB queries under minutes which would otherwise take hours if not optimized. It's a better practice to create a rake