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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:52:14+00:00 2026-05-31T02:52:14+00:00

(sorry for any bad English) Let’s suppose I have models A, B, C. Each

  • 0

(sorry for any bad English)

Let’s suppose I have models A, B, C. Each model have one address.

In the book “SQL Antipatterns: Avoiding the Pitfalls of Database Programming” (chapter 7 – Polymorphic Associations)
there is a recipe to avoid such associations through the use of a “common super table” (also called base table or ancestor table).

Polymorphically, it would be:

table addresses:
    id: integer
    parent_type:string  # 'A', 'B' or 'C'
    parent_id: integer

I know you can use intersection tables, but the following solution looks more polished:

Instead of polymorphically associating A, B, C with Address, the recipe suggests to create a super table (Addressing) that have only a id field (surrogate key or pseudo key). Then, the other tables references Addressing. That way, says the author, “you can rely on the enforcement of your database’s data integrity by foreign keys”.
So, it would be:

table addressing
    id: integer
table addresses
    id: integer
    addressing_id: integer  (foreign_key)
    zip: string
table a
    id: integer
    addressing_id: integer  (foreign_key)
    name: string
table b
    id: integer
    addressing_id: integer  (foreign_key)
    name: string
table c
    id: integer
    addressing_id: integer  (foreign_key)
    name: string

The SQL query would be like this:

SELECT * from a
  JOIN address USING addressing_id
  WHERE a.addressing_id = 1243 

The QUESTION is: how to code such scenario in Rails ? I’ve tried in several ways without success.

  • 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-31T02:52:15+00:00Added an answer on May 31, 2026 at 2:52 am

    Do objects A, B and C each have a single address? Or many addresses? From your comment below it looks like each object has one address.

    If every object has just one address you can simply put a foreign key in the A/B/C objects with the address ID. Let the House or Office objects have one address:

    class House < ActiveRecord::Base
      belongs_to :address
    end
    
    class Office < ActiveRecord::Base
      belongs_to :address
    end
    

    Your offices and houses DB tables need to have a foreign key address_id.
    This way you can access an object’s address with something like house.address or office.address.

    If these objects could have many addresses, the solution depends on the A/B/C objects. If they are related you could use Single Table Inheritance – Rails supports this pattern well – but without more information it’s difficult to say which is the best approach.

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

Sidebar

Related Questions

Hi (sorry for my bad english :p) Imagine these models : class Fruit(models.Model): #
Sorry I cannot post any source code... I have a code running a master/slave
sorry in advance for any orthographic mistakes (english is not my first language). I
Sorry about the extremely vague question title (any suggestions for improvements welcome) I have
Sorry for bad english. bfproduct is a table and productid is primary key in
First : Appologize for my bad english. Sorry for this newbie software question, but
Hi to all of stackoverflow, i'm Italian so, i'm sorry for my bad English...
(Sorry for my bad English) I want to select all Table Names in my
(sorry for my bad english) In a Silverlight 4 + RIA Services + EF4
Sorry for my bad English... Using Delphi 7 I want to create a dialog

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.