Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 768325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:09:52+00:00 2026-05-14T18:09:52+00:00

http://github.com/collectiveidea/awesome_nested_set awesome nested set plugin and currently, if I choose a sub category as

  • 0

http://github.com/collectiveidea/awesome_nested_set

awesome nested set plugin and currently, if I choose a sub category as my category_id for an item, I can not search by its parent.

Category.parent
     Category.Child

I choose Category.child as the category that my item is in. So now my item has category_id of 4 stored in it.

If I go to a page in my rails application, lets say the Category page and I am on the Category.parent’s page, I want to show products that have category_id’s of all the descendants as well.

So ideally i want to have a find method that can take into account the descendants. You can get the descendants of a root by calling root.descendants (a built in plugin method).

How would I go about making it so I can query a find that gets the descendants of a root instead of what its doing now which is binging up nothing unless the product had a specific category_id of the Category.parent.

I hope I am being clear here. I either need to figure out a way to create a find method or named_scope that can query and return an array of objects that have id’s corresponding tot he descendants of a root OR if I have any other options, what are they?

I thought about creating a field in my products table like parent_id which can keep track of the parent so i can then create two named scopes one finding the parent stuff and one finding the child stuff and chaining them.

I know I can create a named scope for each child and chain them together for multiple children but this seems a very tedious process and also, if you add more children, you would need to specify more named scopes.

//this is what i tried first
@category = Category.find(params[:id])
@child_products = @category.descendants.products

//this actually works but only for one
@child_products = @category.descendants.first.products

because a product belongs_to a Category and a Category has many products. However, the descendants function gives back an array of all the descendants. How do I filter through those and get the combined products?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:09:52+00:00Added an answer on May 14, 2026 at 6:09 pm

    Move the named scope to your Product model, since that’s what you’re trying to display.

    # product.rb    
    named_scope :for_category_ids, lambda {|ids| {:conditions => {:category_id => ids}}}
    

    Then in your controller:

    @products = Product.for_category_ids(@category.descendants.map(&:id))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 399k
  • Answers 399k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Have you tried doing it in java yourself? I tried… May 15, 2026 at 3:57 am
  • Editorial Team
    Editorial Team added an answer Setting Thread.CurrentCulture will affect formatting and parsing of dates and… May 15, 2026 at 3:57 am
  • Editorial Team
    Editorial Team added an answer Trying to find anything in the standard that would spell… May 15, 2026 at 3:57 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.