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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:45:41+00:00 2026-06-11T23:45:41+00:00

It has been almost a week since I’m trying to find a solution to

  • 0

It has been almost a week since I’m trying to find a solution to my confusion… Here it is:

I have a Program model.
I have a ProgramCategory model.
I have a ProgramSubcategory model.

Let’s make it more clear:

ProgramCategory ======> Shows, Movies, 
ProgramSubcategory ===> Featured Shows, Action Movies
Program ==============> Lost, Dexter, Game of Thrones etc...

I want to able to associate each of these models with eachother. I’ve got what I want to do particularly with many-to-many association. I have a categories_navigation JOIN model/table and all of my other tables are connected to it. By this way, I can access all fields of all of these models.

BUT…

As you know, has_many :through style associations are always plural. There is nothing such as has_one :through or belongs_to through. BUT I want to play with SINGULAR objects, NOT arrays. A Program has ONLY ONE Subcategory and ONLY ONE Category. I’m just using a join table to only make connection between those 3. For example, at the moment I can access program.program_categories[0].title but I want to access it such like program.program_category for example.

How can I have ‘has_many :through’s abilities but has_one’s singular usage convention all together? 😐

P.S: My previous question was about this situation too, but I decided to start from scratch and learn about philosophy of associations. If you want so you may check my previous post here: How to access associated model through another model in Rails?

  • 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-11T23:45:42+00:00Added an answer on June 11, 2026 at 11:45 pm

    Why a join table where you have a direct relationship? In the end, a program belongs to a subcategory, which in turn belongs to one category. So no join table needed.

    class Program < ActiveRecord::Base
      belongs_to :subcategory   # references the "subcategory_id" in the table
      # belongs_to :category, :through => :subcategory
      delegate :category, :to => :subcategory
    end
    
    class Subcategory < ActiveRecord::Base
      has_many :programs
      belongs_to :category    # references the "category_id" in the table
    end
    
    class Category < ActiveRecord::Base
      has_many :subcategories
      has_many :programs, :through => :subcategories
    end
    

    Another point of view is to make categories a tree, so you don’t need an additional model for “level-2” categories, you can add as many levels you want. If you use a tree implementation like “closure_tree” you can also get all subcategories (at any level), all supercategories, etc

    In that case you skip the Subcategory model, as it is just a category with depth=2

    class Program < ActiveRecord::Base
      belongs_to :category   # references the "category_id" in the table
    
      scope :in_categories, lambda do |cats|
        where(:category_id => cats)  # accepts one or an array of either integers or Categories
      end
    end
    
    class Category < ActiveRecord::Base
      acts_as_tree
      has_many :programs
    end
    

    Just an example on how to use a tree to filter by category. Suppose you have a select box, and you select a category from it. You want to retrieve all the object which correspond to any subcategory thereof, not only the category.

    class ProgramsController < ApplicationController
    
      def index
    
        @programs = Program.scoped
        if params[:category].present?
          category = Category.find(params[:category])
          @programs = @programs.in_categories(category.descendant_ids + [category.id])
        end
    
      end
    
    end
    

    Tree-win!

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

Sidebar

Related Questions

I have a code that has been working for almost 4 years (since boost
I'm almost positive this has been answered already; however, unable to find it since
Although I am almost sure this has been covered, I can't seem to find
I have a game on Facebook that has been running successfully for almost a
i have an almost simular problem as posted before. My first problem has been
It's been almost a week since I've began to dig deeper in forms ,
It has been asked when MeteorJS will have auth in place... I think everyone
this has been an ongoing problem with me, ive been trying to make a
This has been a rather problematic issue on numerous occasions. We have alot of
This has been driving me crazy for the past few minutes I have a

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.