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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:41:30+00:00 2026-05-24T22:41:30+00:00

I have a Store that has many Catalogs and in turn each catalog has

  • 0

I have a Store that has many Catalogs and in turn each catalog has many Products.

The Product and Catalog shares many to many relationship.
One product can belong to many catalogs and vice-verse.

So, my model definition goes like this:

class Store < ActiveRecord::Base
   has_many :store_catalogs
   has_many :catalogs, :through => :store_catalogs
end

class StoreCatalog < ActiveRecord::Base
  belongs_to :store
  belongs_to :catalog
end

class Catalog < ActiveRecord::Base
    has_many :store_catalogs
    has_many :stores, :through => :store_catalogs
    has_and_belongs_to_many :product_set, :class_name => "Product"
 end    

class Product < ActiveRecord::Base
   has_and_belongs_to_many :catalogs
end

Now, I would like to access all the products that belongs to a store.
How do I link those together so that I get that? please suggest.

I am trying out various combinations from the rails console to pull the data, not sure if the console itself limits the relation based querying by any chance(though i would like to believe it doesn’t).

  • 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-24T22:41:31+00:00Added an answer on May 24, 2026 at 10:41 pm

    I think this should solve your problem

    class Store < ActiveRecord::Base
      has_many :catalogs
      has_many :products, :through => :catalogs
    end
    
    class Catalog < ActiveRecord::Base
      belongs_to :store
      has_and_belongs_to_many :products
    end
    
    class Product < ActiveRecord::Base
      has_and_belongs_to_many :catalogs
    end
    

    And then you just need a table in your database called catalogs_products with catalog_id and product_id to link the has_and_belongs_to_many association.

    Then in order to get all the products from a store just do

    Store.find(id).products
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a store model that has many products with a has_many :through relationship.
Let's say I have a class called Store that has many Employees. My RESTful
Okay so I have a class that has 'weak typing' I.E. it can store
I have the following relationship: store.rb -> has_many :products product.rb -> belongs_to :store routes.rb
I have a managed object subclass named Tag that has a to-many relationship to
I have an entity called guestlist that has a 1 to many relationship to
Say that I have many blogs, and each blog has_many posts. These are stored
Lets say I have an object that has stringProp1, stringProp2. I wish to store
I have a table that has two records (there will be many at runtime).
I have 3 models defined: Palette (has many swatches) Swatch (has one color) Color

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.