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 9063445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:00:22+00:00 2026-06-16T16:00:22+00:00

I have a category model, which is basically what products will be assigned to.

  • 0

I have a category model, which is basically what products will be assigned to.

So my category model looks like this:

attr_accessible :name
has_many :category_products do
  def with_products
    includes(:product)
  end
end

has_many :products, :through => :category_products

What I would like to do is to have categories within categories – so a product should be able to be put in Men and then Shoes.

That way, I can have a Men drop-down menu, that produces all the subcategories within Men like Shoes.

But, if a user clicks on Men they will see all the products in that category.

Thoughts?

  • 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-06-16T16:00:23+00:00Added an answer on June 16, 2026 at 4:00 pm

    You can use a gem like Ancestry to organize your categories into a hierarchy.

    If you only need one level deep of nesting, you can use a self-join.

    belongs_to :parent_category, class: "Category"
    has_many :subcategories, class: "Category"
    

    You’ll also need a migration to add category_id to your category table.

    Stick a drop down in your app/views/categories/_form.html.erb for category_id and populate it with your list of Categories. You can use this to select a “Parent” for your categories.

    Then you can do stuff like:

    # Get parent category's name
    <%= somecategory.parent.name %>
    
    # Iterate through subcategories
    <% someothercategory.subcategories.each do |category| %>
      <%= category.name %>
    <% end %>
    

    Using a gem like Ancestry will give you a lot more flexibility when it comes to working with these categories and their associations though.

    A bit about how Ancestry works:

    Ancestry stores a path from the root to the parent for every node.
    This is a variation on the materialised path database pattern. It
    allows Ancestry to fetch any relation (siblings, descendants, etc.) in
    a single SQL query without the complicated algorithms and
    incomprehensibility associated with left and right values.
    Additionally, any inserts, deletes and updates only affect nodes
    within the affected node’s own subtree.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Category model, which has many Products . I would like to
I have a model Category, which has_many Products, and a Product in turn has_many
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have hierarchical data stored in the datastore using a model which looks like
I need to have a model which will behave like a embedded and not-embedded.
I have a Category model which uses acts_as_tree class Category < ActiveRecord::Base acts_as_tree :order=>name
I have a Model Category which has name:string categoryId:integer parent_id:integer the categoryId doesnt matter
I have one model User and will be creating another one Category . Both
I have a Category model, and a Project model, which contains a ForeignKey to
I have very basic category model ID, RootCategoryID, Name and if I have category

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.