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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:30:39+00:00 2026-05-29T20:30:39+00:00

I have recently been advised that for my current rails app relationships I should

  • 0

I have recently been advised that for my current rails app relationships I should use the gem nested set. ( My previous thread / question here) I currently have 3 models,

Categories has_many Subcategories
Subcategories belongs_to Categories, and has_many products.
Product belongs_to Subcategory. I wanted to display it something like this

+Category

—-Subcategory

——–Product

——–Product

—-Subcategory

——–Product

——–Product

+Category

—-Subcategory

——–Product

——–Product

So if I were to do this in nested_set, how would I set this up in my Models? Would I remove my subcategory and product models, and just add acts_as_nested_set in the Category model? and once I have the model taken care of, what will I update my controllers actions with, to be able to create nodes in the nested set I create?

I guess just help me understand how I can do the CRUD, create, read, update, and destroying of this nested_set list.

Here is some code I have already

Categories Controller:

class CategoriesController < ApplicationController
def new
  @category = Category.new
  @count = Category.count
end

def create
@category = Category.new(params[:category])
if @category.save
  redirect_to products_path, :notice => "Category created! Woo Hoo!"
else
  render "new"
end
end

def edit
  @category = Category.find(params[:id]) 
end

def destroy
  @category = Category.find(params[:id])
  @category.destroy
  flash[:notice] = "Category has been obliterated!"
  redirect_to products_path
end

def update
  @category = Category.find(params[:id])

if @category.update_attributes(params[:category])
  flash[:notice] = "Changed it for ya!"
  redirect_to products_path
else 
  flash[:alert] = "Category has not been updated."
  render :action => "edit"
end
end

def show
  @category = Category.find(params[:id])
end

def index
  @categories = Category.all
end 
end

Category Model:

class Category < ActiveRecord::Base
  acts_as_nested_set
  has_many :subcategories
  validates_uniqueness_of :position
  scope :position, order("position asc")

end

Subcategory Model:

class Subcategory < ActiveRecord::Base
  belongs_to :category
  has_many :products
  scope :position, order("position asc")
end

And finally, Product Model:

class Product < ActiveRecord::Base
  belongs_to :subcategory
  has_many :products
  scope :position, order("position asc")
end

Any help would be very appreciated.

  • 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-29T20:30:41+00:00Added an answer on May 29, 2026 at 8:30 pm

    I would go with a Category and a Product like so:

    class Product > ActiveRecord::Base
      belongs_to :category
    end
    
    class Category > ActiveRecord::Base
      has_many :products
      acts_as_nested_set
    end
    
    class CategoryController < ApplicationController
       def create
    
          @category = params[:id] ? Category.find(params[:id]).children.new(params[:category]) : Category.new(params[:category])
    
          if @category.save
              redirect_to products_path, :notice => "Category created! Woo Hoo!"
          else
              render "new" 
          end
       end
    
       def new
          @category = params[:id] ? Category.find(params[:id]).children.new : Category.new
       end
    
       def index
          @categories = params[:id] ? Category.find(params[:id]).children : Category.all
       end
    end
    
    #config/routes.rb your categories resource could be something like..
    resources :categories do
       resources :children, :controller => :categories, 
                                  :only => [:index, :new, :create]
    end
    

    this way is the most flexible, as you can put your products in any a category at any level.

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

Sidebar

Related Questions

I have recently been trying to upgrade my app form Rails 2.3.8 to newly-releases
I have recently been working with someone on a project that is very ajax
I have recently been working with a COM+ component that processes an input XML
I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails
I have recently been stuck on what I think should be a fairly simple
I have recently been running into many different areas of SQL Server that I
I have recently been reading about the general use of prime factors within cryptography.
I have recently been having problems with my app and I need to view
I have recently been using rails for a web application, and so I have
I have recently been working on a nodejs app which uses open socket connections

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.