Rails strict_loading mode is to stop lazy loading of associations.
When the strict_loading
mode is used,
the related information should be keen loaded utilizing contains
else a ActiveRecord::StrictLoadingViolationError
shall be raised.
This mode helps in figuring out
and fixing N+1 question points by making certain that sure associations are loaded eagerly to keep away from efficiency bottlenecks.
class Shopper < ApplicationRecord
has_many :initiatives
finish
Shopper.strict_loading.first.initiatives
# raises ActiveRecord::StrictLoadingViolationError
class Shopper < ApplicationRecord
has_many :initiatives, strict_loading: true
finish
Shopper.first.initiatives
# raises ActiveRecord::StrictLoadingViolationError
Shopper.contains(:initiatives).first.initiatives
Shopper Load (0.7ms) SELECT "shoppers".* FROM "shoppers" ORDER BY "shoppers"."id" ASC LIMIT $1 [["LIMIT", 1]]
Undertaking Load (2.4ms) SELECT "initiatives".* FROM "initiatives" WHERE "initiatives"."client_id" = $1 [["client_id", 1]]
# =>
[#<Project:0x00000011133aaa48
id: 1,
client_id: 1,
name: "Miru",
description: "Time tracking">,
#<Project:0x0000001111f150b0
id: 2,
client_id: 1,
name: "Azure.com",
description: "Cloud Computing">
]
Earlier than
Strict loading in :n_plus_one_only
mode is particularly designed to deal with efficiency points that may come up when navigating by means of deeply nested associations
It permits direct loading of associations whereas proscribing deeper traversal, stopping potential N+1 question points
and surprises associated to ordering inconsistencies.
shopper = Shopper.discover(1)
shopper.strict_loading!(mode: :n_plus_one_only)
shopper.initiatives.first
# SELECT "initiatives".* FROM "initiatives" WHERE "initiatives"."client_id" = $1 [["client_id", 1]] -- non-deterministic order
The place as navigating by means of deeply nested associations throws error
shopper.initiatives.first.timesheets # raises ActiveRecord::StrictLoadingViolationError
After
Strict loading utilizing :n_plus_one_only
doesn’t eagerly load youngster associations.
With this change, youngster associations are not eagerly loaded, to match supposed conduct
and to stop non-deterministic order points brought on by calling strategies like first or final. As first
and final don’t trigger an N+1 by themselves, calling youngster associations will not elevate.
It means associations are eagerly loaded
and youngster associations are lazy loaded.
shopper = Shopper.discover(1)
shopper.strict_loading!(mode: :n_plus_one_only)
shopper.initiatives.first
# SELECT "initiatives".* FROM "initiatives" WHERE "initiatives"."client_id" = $1 [["client_id", 1]] -- non-deterministic order
shopper.initiatives.first.timesheets # not raises error
Abstract
With Strict loading, we will stop lazy loading of associations because it raises ActiveRecord::StrictLoadingViolationError
if the related information will not be keen loaded.
However With strict_loading!(mode: :n_plus_one_only)
, associations are eagerly loaded
and youngster associations are lazy loaded.