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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:36:49+00:00 2026-05-13T18:36:49+00:00

Looking for some guidance on the best way to implement this scenario: I have

  • 0

Looking for some guidance on the best way to implement this scenario:

I have an items table (of products) and want to support the ability to cross-sell / up-sell / complement items. So there is an item-to-item(s) relationship here. In this join table I need to include additional attributes beyond the keys such as the sales_relation between the items (e.g., cross, up, complement, substitute, etc).

How do I go about setting up the Model associations?

  • 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-13T18:36:49+00:00Added an answer on May 13, 2026 at 6:36 pm

    By the sounds of it, this join table represents a whole new model. I’m not sure exactly what your requirements are, but I’ll play out one potential solution. For now, let’s call the join model a SalesRelationship.

    I’m going to call the item/product objects “products”, since to me it’s a little less generic.

    The migration for this would look something like:

    class CreateSalesRelationship < ActiveRecord::Migration
      def self.up
        create_table :sales_relationship |t|
          t.string :product_id
          t.string :other_product_id
          t.string :type
          t.timestamps
        end
      end
    
      def self.down
        drop_table :sales_relationship
      end
    end
    

    You can include any other attributes necessary in that migration as well. Next, create a SalesRelationship model:

    class SalesRelationship < ActiveRecord::Base
      belongs_to :product
      belongs_to :other_product, :class_name => "Product
    end
    

    Then, create subclasses for the different types of relationship:

    class CrossSell < SalesRelationship
    end
    
    class UpSell < SalesRelationship
    end
    
    class Complement < SalesRelationship
    end
    
    class Substitute < SalesRelationship
    end
    

    Then set up the relationships on the Product model:

    class Product < ActiveRecord::Base
      has_many :sales_relationships, :dependent => :destroy
      has_many :cross_sells
      has_many :up_sells
      has_many :complements
      has_many :substitutes
    
      has_many :cross_sale_products, :through => :cross_sells, :source => :other_product
      has_many :up_sale_products, :through => :up_sells, :source => :other_product
      has_many :complementary_products, :through => :complements, :source => :other_product
      has_many :substitute_products, :through => :substitutes, :source => :other_product
    end
    

    Now you should be able to create and add related products all you want.

    @product1.substitute_products << @product2
    new_product = @product2.complementary_products.build
    

    For extra credit, you could write a simple validation on the SalesRelationship model that ensures a product is never related to itself. That may or may not be necessary, depending on your requirements.

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

Sidebar

Related Questions

Looking for some advice on the best way to implement localization along with client
Hi I am looking for some guidance on this... I want to change my
I'm looking for some guidance on the best way to authenticate to my WebService.
Looking for some guidance on this issue (below is a parallel example): I have
Looking for some best-practice guidance. Let's say I have a line of code like
Looking for some guidance on a WCF service I’m prototyping. I have a WCF
Being a self-confessed newbie I'm looking for some advice and guidance :) I have
I'm looking for some guidance on how I can recreated a this feature. what
I'm looking for some guidance on how I can recreate this feature. As you
Hi guys looking for some guidance, i have a knowledge of xhtml, css, javascript,

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.