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

Is there any way to get all roots for given collection? #148

Closed
timurkhafizov opened this issue Feb 7, 2015 · 1 comment
Closed

Is there any way to get all roots for given collection? #148

timurkhafizov opened this issue Feb 7, 2015 · 1 comment
Labels

Comments

@timurkhafizov
Copy link

I mean if I have some Category relation then I want to call something like roots on this collection and get roots only for given categories not the all root categories.

I tried to create custom class method but had no luck:

def self.tree_parents
  base_realtion = Category.none
  root_category_ids = []

  all.each do |category|
    if category.root?
      root_category_ids = []
    else
      base_relation = base_relation.merge(category.self_and_ancestors.where(_ct.parent_column_name.to_sym => nil))
    end
  end

  base_relation.merge(Category.where(id: root_category_ids))
end

I would appreciate any help.
Thank you!

@timurkhafizov
Copy link
Author

Closing it right now. I have managed logic mistake in my code and going to fix it

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

No branches or pull requests

2 participants