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

  • Home
  • SEARCH
  • 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 4080830
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:58:51+00:00 2026-05-20T17:58:51+00:00

i have a problem which is i believe basic for most of the RoR

  • 0

i have a problem which is i believe basic for most of the RoR developers. In fact, it’s more about “understanding” than really programming it (i have already been programming a few modules to be able to extend ActiveRecord::Base to allow easy generation of Ext-JS related things without any problem).

Let’s take a simple example, i’ll talk in an ORM oriented language rather than a database / model language.

  • ThirdParty is a basic entity, having a common member named ‘label’.
  • A ThirdParty instance can be a Customer, a Supplier, both, or none of them (So Customer / Supplier are like concrete interfaces rather than inherited classes).
  • Both Customer and Supplier have specialized members, which are for most of them unique to each respective class (Customer / Supplier don’t share much data except the label field they are bound to via ThirdParty).

i have tried to do various things:

  • ThirdParty < ActiveRecord::Base
  • Customer < ThirdParty
  • Supplier < ThirdParty

[EDIT]
Missed my explanation here. i meant: one single instance of ThirdParty could be a customer, a supplier, BOTH, OR NONE.
As the first commenter pointed out, my example might not be the best… You can replace “ThirdParty” by “Character”, and “Customer” / “Supplier” by “Archer” / “SwordsMan”. A character can either be one, two, both, or none, but would provide the information on any instance of Character. The getters on Archer / SwordsMan shall return instances of Character “implementing” the interface contract, rather than just returning an instance of themself with a “getter” on the character object.
[/EDIT]

Also i have tried:

  • ThirdParty < ActiveRecord::Base (has_one Customer; has_one Supplier)
  • Customer < ActiveRecord::Base (belongs_to ThirdParty)
  • Supplier < ActiveRecord::Base (belongs_to ThirdParty)

But it seems a bit “ugly” to me, because i have to do Customer.find(something).third_party.label to access data “inherited” by my “interfaces”.

Also, i could include a customer_id and a supplier_id field on my ThirdParty model, but it doesn’t seem right neither, because one ThirdParty can be both, or none of them…

And of course, i’d like to be able to find a solution which isn’t obstructive / too much of a hack, to allow me for instance to then do some polymorphic association for another model (Such as an addressable, or commentable, contactable, etc…)

i also have been reading a lot on the subject like on such blog: http://mediumexposure.com/multiple-table-inheritance-active-record/ … But i’am lost in the right way to do it (On most of the documentation i have found on the subject isn’t clear if it’s for Rails 1, 2, 3… What’s recently been implemented on the matter, etc).

Can someone provide me with various “up-to-date” websites or to an example fitting the example i have given?

By advance, i thank you all for having red my question, and to edventually explain me the different techniques used regarding this particular subject.

P.S.: If you need me to explain more what i’m looking for, i can try… But keep in mind that my english is a bit “limited” on this domain, since i’m relatively new to RoR.

  • 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-20T17:58:52+00:00Added an answer on May 20, 2026 at 5:58 pm

    Having only one field in common, single (or multiple) table inheritance probably isn’t the way to go. If you want to share functionality between the classes I would create a ThirdParty module that gets included into each class:

    module ThirdParty
      def self.included(base)
        base.extend ClassMethods
      end
    
      module ClassMethods
        # define your shared class methods here
      end
    
      # define you shared instance methods down here
    end
    
    class Customer < ActiveRecord::Base
      include ThirdParty
    end
    
    class Supplier < ActiveRecord::Base
      include ThirdParty
    end
    

    This allows you to define methods that utilize the label attribute of each including class. You mention that you’d like to be able to interface with other models, possibly polymorphically. If these associations are present in all ThirdParty objects then you would just setup the associations in either the self.included call (using instance_eval) or in ClassMethods as a method:

    module ThirdParty
      def self.included(base)
        base.extend ClassMethods
        base.instance_eval do
          has_many :buyers, :as => :third_party
        end
      end
    
      module ClassMethods
        def has_address
          has_one :address, :as => :third_party
          delegate :street, :city, :state, :zip, :to => :address
        end
      end
    end
    
    class Supplier < ActiveRecord::Base
      include ThirdParty # this would set up the :buyers association
    
      has_address # this sets up the address association
    end
    

    Hopefully that helps. For more information on doing stuff like this just google “ruby mixin” – there’s ton of info out there.

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

Sidebar

Related Questions

I have a problem which requires me to parse several log files from a
I have a problem which I think is related to forward declarations, but perhaps
I have a very simple problem which requires a very quick and simple solution
I have a problem with a string in C++ which has several words in
I have a problem with an application which uses the same stored procedure over
I have a problem with a little .Net web application which uses the Amazon
I have a problem in my project with the .designer which as everyone know
I have a problem, which I do not seem to be able to solve...
The problem: I have a class which contains a template method execute which calls
Consider this problem: I have a program which should fetch (let's say) 100 records

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.