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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:19:18+00:00 2026-06-09T09:19:18+00:00

That title sounds weird, so let me try to explain what it is I’m

  • 0

That title sounds weird, so let me try to explain what it is I’m trying to accomplish.

Let’s say I have a Category model, and the structure looks a little like this:

categories

id

name

parent_id


I also have a model for articles

articles

id
category_id


If I have a category named “News” with id: 1, and then a subcategory for it named “World News” with the id: 2 and parent_id: 1, if I have an article with category_id: 1 and another article with category_id: 2, I would like to be able to do something like:

category = Category.find 2 # This is the subcategory
category.articles # => Shows just the article belonging to that category

And

category = Category.find 1 # This is the parent category
category.articles # => Shows BOTH articles, the one for the child and the one for parent

Does that make sense?

  • 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-09T09:19:20+00:00Added an answer on June 9, 2026 at 9:19 am

    What you are looking for is a tree structure for your categories. Using parent_id is the most obvious way to implement it, but it gets complicated when you want to query children.

    A better solution is to use a nested set, which adds a bit of complication on insert/delete but pays off with very simple queries.

    You can do the research to implement this, but as with a lot of things in rails, it’s a solved problem. Use awesome_nested_set which will take care of all of this for you.

    Once you have installed the gem, create this migration:

    def up
      add_column :categories, :lft, :integer
      add_column :categories, :rgt, :integer
    
      Category.reset_column_information!
      Category.rebuild!
    end
    
    def down
      remove_column :categories, :lft
      remove_column :categories, :rgt
    end
    

    Then add this line to your model:

    class Category < ActiveRecord::Base
      acts_as_nested_set
    
      # ...
    end
    

    You can use a query like this to get all articles:

    category = Category.find(1) # Or any category id
    
    category.self_and_descendants.articles
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I apologize if the title sounds abstruse, let me make that simple with an
That title is presumably awfully worded. I have some PostgreSQL tables. There is a
(I realize that my title is poor. If after reading the question you have
I know that title sounds crazy but here is my situation. After a certain
Ok, the title may sound a little weird, but I think that kinda describe
Yes, title sounds a bit confusing, so I'll explain what I mean: suppose you
I know the title sounds easy but I am looking for an example that
I am sorry, that sounds like a noob question. I am trying to do
Wow, that title was a mouthful... I'm sure that sounds very confusing at first
This sounds like a weird title and probably not stated too well. But here's

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.