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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:08:37+00:00 2026-05-26T15:08:37+00:00

Here is an example of the structure I’m trying to achieve. I’ll just setup

  • 0

Here is an example of the structure I’m trying to achieve. I’ll just setup a silly example so it’s easier to explain:

Let’s say we have a Product that holds a bunch of different hammers. Each hammer has multiple attributes (weight, size, color, etc) but they all can hammer nails in. So in that respect they are interchangeable. However they are not interchangeable with other products, like a chainsaw or a sledgehammer.

So I want to be able to keep a list of all products that are interchangeable. So if one hammer is not available I could see ids all the other products that I can give to a customer instead.

Since I don’t know how many interchangeable products each product can have ( let’s say there are 5 different hammers, and 50 screwdrivers), I can’t just create an interchangeable field to keep this info. i was thinking about enums, but they are more complicated to run reports on.

Here is what I have so far, but I’m not sure if this is the best solution (it’s kind of late and my mind is starting to melt – this is a much simplified example if my self referential tables):

class Product < ActiveRecord::Base

  has_many :interchangable_products, :dependent => :destroy
end


class InterchangableProduct < ActiveRecord::Base
  belongs_to :product, :class => "Product", :foreign_key => :product_id


  belongs_to :interchangable_with, :class_name => "InterchangableProduct", :foreign_key => :interchangable_with_id
  has_many   :interchangables, :class_name => "InterchangableProduct",  :inverse_of => :interchangable_with, :foreign_key => :interchangable_with_id

  validates :product_id, :presence => true, :uniqueness => [:scope => :interchangable_with_id]
end

Thank you

  • 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-26T15:08:37+00:00Added an answer on May 26, 2026 at 3:08 pm

    Thanks to a comment from Dave Newton I arrived to a solution that looks sort of like this:

    class InterchangeableProductRelationship < ActiveRecord::Base
      belongs_to :product, :class_name => "Product", :foreign_key => "product_id"
      belongs_to :interchangeable_product, :class_name => "Product", :foreign_key => "interchangeable_product_id"
    
      validates :product_id, :presence => true, :uniqueness => [:scope => :interchangeable_product_id]
    end
    
    
    class Product < ActiveRecord::Base
      has_many :relations_to, :foreign_key => 'product_id', :class_name => "InterchangeableProductRelationship"
      has_many :relations_from, :foreign_key => 'interchangeable_product_id', :class_name => "InterchangeableProductRelationship"  
      has_many :linked_to, :through => :relations_to, :source => :interchangeable_product
      has_many :linked_from, :through => :relations_from, :source => :product
    
    
      def interchanges_with
        self.linked_to | self.linked_from
      end 
    end
    

    Hopefully this will be helpful to someone.

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

Sidebar

Related Questions

For example I have a file name call A.java. Here its structure: public class
Here an example http://jsfiddle.net/7aVXc/ Let' say current image height is 400px . How do
Here is the example with comments: class Program { // first version of structure
Here is example which I have try <?php include 'spider/classes/simple_html_dom.php'; $html = new simple_html_dom();
Here is an example of my source control structure in TFS 2010: TFS Project
I have a Code example here. struct node { int data; struct node *link;
I can't use the example here because it's specific to the structure the asker
Here is the example structure of the table: ID Name ParentID ----------------------- 1 Ancestor
Here an example http://jsfiddle.net/EhLsT/ $(window).scroll(function () { if ($(window).scrollTop() > $(#header).offset().top) { $(#floating).show(); }
Here an example http://jsfiddle.net/NuzDj/ If you resize windows size, sidebar overlapping the wrapper &

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.