You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a case in which i have multiple sets of self_and_ancestors results. The amount of these sets depends on the variable which makes it unpredictable and N+1 prone therefore i was looking for some ways of simplifying that with one query. Let's say my code looks as follows:
taxons = []
product.taxons.each do |taxon|
taxon.self_and_ancestors.each { |t| taxons << t }
end
Which obviously produces new query for each self_and_ancestors call. So if there are 100 product.taxons, 100 queries will be produced.
I was experimenting with doing that with merge:
collection = Spree::Taxon.all
product.taxons.each do |taxon|
collection.merge!(taxon.self_and_ancestors)
end
which indeed produced one single query but it operates on lft and rgt columns and i have no idea if that's proper way of handling that and/or this produced sql query is valid. In fact, it merged where statements for all of the self_and_ancestors calls.
For your convenience, i prepared an example for you. Let's say product.taxons returns two taxons. Here are the sql queries produced for each one of them:
product.taxons.first.self_and_ancestors.to_sql
# => SELECT "spree_taxons".* FROM "spree_taxons" WHERE "spree_taxons"."lft" <= 1 AND "spree_taxons"."rgt" >= 232 ORDER BY lft
product.taxons.last.self_and_ancestors.to_sql
# => SELECT "spree_taxons".* FROM "spree_taxons" WHERE "spree_taxons"."lft" <= 201 AND "spree_taxons"."rgt" >= 202 ORDER BY lft
And here is the result of merging them:
collection = Spree::Taxon.all
product.taxons.each do |taxon|
collection.merge!(taxon.self_and_ancestors)
end
collection.to_sql
# => SELECT "spree_taxons".* FROM "spree_taxons" WHERE "spree_taxons"."lft" <= 201 AND "spree_taxons"."rgt" >= 202 ORDER BY lft
Does that look valid to you? Is that how i should approach that?
The text was updated successfully, but these errors were encountered:
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
Hello!
I have a case in which i have multiple sets of
self_and_ancestors
results. The amount of these sets depends on the variable which makes it unpredictable and N+1 prone therefore i was looking for some ways of simplifying that with one query. Let's say my code looks as follows:Which obviously produces new query for each
self_and_ancestors
call. So if there are 100product.taxons
, 100 queries will be produced.I was experimenting with doing that with
merge
:which indeed produced one single query but it operates on
lft
andrgt
columns and i have no idea if that's proper way of handling that and/or this produced sql query is valid. In fact, it mergedwhere
statements for all of theself_and_ancestors
calls.For your convenience, i prepared an example for you. Let's say
product.taxons
returns two taxons. Here are the sql queries produced for each one of them:And here is the result of merging them:
Does that look valid to you? Is that how i should approach that?
The text was updated successfully, but these errors were encountered: