simon at urbanautomaton.com
Tue Aug 4 04:28:49 PDT 2020
I think the problem might be that eager loading is applied to specific
associations, and the ones you're applying eager loading to aren't the ones
that you're using to perform your reads.
Specifically, with `some_beta_scope.includes(gammas: :deltas)` you're eager
loading the `Beta#gammas` and `Gamma#deltas` associations, but in your view
you're using `Beta#deltas` and `Delta#gamma` to actually load the
You'd either need to rewrite the eager load to match your access pattern
(`some_beta_scope.includes(deltas: :gamma)`, I *think*), or rewrite the
loop to match the eager loading, e.g.
- @betas.each do |beta|
- beta.gammas.each do |gamma|
- gamma.deltas.each do |delta|
= gamma.name <http://delta.gamma.name/>
Does that work?
On Tue, 4 Aug 2020 at 12:14, Andrew Stewart <boss at airbladesoftware.com>
> Hello LRUG!
> I am having trouble getting rid of database calls to read intermediate
> objects in a hierarchy.
> I have some models like this:
> class Alpha < ApplicationRecord
> has_many :betas
> class Beta < ApplicationRecord
> belongs_to :alpha
> has_many :gammas
> has_many :deltas, through: :gammas
> class Gamma < ApplicationRecord
> belongs_to :beta
> has_many :deltas
> class Delta < ApplicationRecord
> belongs_to :gamma
> And a controller action like this:
> alpha = Alpha.first
> @betas = alpha.betas
> .joins(gammas: :deltas) # exclude betas without deltas
> .includes(gammas: :deltas)
> And a view (pseudocode):
> - @betas.each do |beta| # loads the @betas relation once
> as expected
> = beta.name
> - beta.deltas.each do |delta| # [A] SQL query to get the
> beta's deltas (once per beta)
> = delta.name
> = delta.gamma.name # [B] SQL query to get the
> delta's gamma (once per delta)
> It seems to me I should be able to avoid the nested queries, [A] and [B],
> because the @betas relation includes the gammas and deltas, but I haven't
> succeeded yet.
> Alternatively, in the console:
> beta = Beta.first
> beta.deltas.first.gamma.name # expected 1 query; actually 2
> I'm using Rails 22.214.171.124 and Ruby 2.7.
> Any pointers would be much appreciated!
> Many thanks,
> Andy Stewart
> Chat mailing list
> Chat at lists.lrug.org
> Archives: http://lists.lrug.org/pipermail/chat-lrug.org
> Manage your subscription: http://lists.lrug.org/options.cgi/chat-lrug.org
> List info: http://lists.lrug.org/listinfo.cgi/chat-lrug.org
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Chat