Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How would one go about eager loading a categories collection's root categories? #337

Open
pugsiman opened this issue Nov 14, 2018 · 1 comment

Comments

@pugsiman
Copy link

pugsiman commented Nov 14, 2018

Say I have a collection of categories, how would I go about eager loading all of their root categories (unless they're the root categories themselves) in one query?

@pugsiman pugsiman changed the title How would one go about eager loading a categories collection's root categories How would one go about eager loading a categories collection's root categories? Nov 14, 2018
@fizvlad
Copy link

fizvlad commented Jun 15, 2022

(The question is 4 years, yeeeeah 🐧)

While solving similar problem, I've ended up using association through scenic view

I have created an SQL view which looked something like this:

SELECT
    DISTINCT ON (descendant_id)
    ancestor_id AS root_id,
    descendant_id AS leaf_id,
    generations AS depth
FROM category_hierarchies
ORDER BY descendant_id, generations DESC

Then I defined a read-only model which uses this view:

# frozen_string_literal: true

class CategoryRootHierarchy < ApplicationRecord
  self.primary_key = nil

  belongs_to :leaf, class_name: 'Category'
  belongs_to :root, class_name: 'Category'

  def readonly?
    true
  end
end

... and added an association to category model:

# frozen_string_literal: true
class Category < ApplicationRecord
  has_closure_tree order: :code, name_column: :code, dependent: :destroy

  has_many :items, dependent: :nullify
  has_one :root_hierarchy, class_name: 'CategoryRootHierarchy', foreign_key: 'leaf_id', inverse_of: :leaf
end

After that I was able to preload the root category:

Item.includes(category: [{ root_hierarchy: [:root] }])

This is probably can be done on Rails level with use of ancestor_hierarchies association, but it will probably look scarier

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants