Rails strict_loading mode is to prevent lazy loading of associations.
When the strict_loading
mode is used,
the associated records will have to be eager loaded using includes
else a ActiveRecord::StrictLoadingViolationError
will be raised.
This mode helps in identifying and fixing N+1 query issues by ensuring that certain associations are loaded eagerly to avoid performance bottlenecks.
Before
Strict loading in :n_plus_one_only
mode is specifically designed to address performance issues that can arise when navigating through deeply nested associations
It allows direct loading of associations while restricting deeper traversal, preventing potential N+1 query issues and surprises related to ordering inconsistencies.
Where as navigating through deeply nested associations throws error
After
Strict loading using :n_plus_one_only
does not eagerly load child associations.
With this change, child associations are no longer eagerly loaded, to match intended behavior and to prevent non-deterministic order issues caused by calling methods like first or last. As first and last don’t cause an N+1 by themselves, calling child associations will no longer raise.
It means associations are eagerly loaded and child associations are lazy loaded.
Summary
With Strict loading, we can prevent lazy loading of associations as it raises ActiveRecord::StrictLoadingViolationError
if the associated records are not eager loaded.
But With strict_loading!(mode: :n_plus_one_only)
, associations are eagerly loaded
and child associations are lazy loaded.