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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:44:49+00:00 2026-05-12T11:44:49+00:00

Given two model classes, Foo and Bar , I want Foo to have 3

  • 0

Given two model classes, Foo and Bar, I want Foo to have 3 references to separate instances of Bar using 3 different property names, with the foreign key on the Foo table. Bar will be managed separately and can belong to many instances of Foo. This somewhat explains it, obviously has_one is the wrong association to use (I think?):

Foo
   has_one :prop_a, :class_name => "Bar"
   has_one :prop_b, :class_name => "Bar"
   has_one :prop_c, :class_name => "Bar"

Bar

There are 3 potential types of Bar, denoted by a bar_type string field, each reference on Foo corresponds to one of these. e.g. Foo.prop_a references an instance of Bar with bar_type = ‘type_a’. How do I create this type of association in Rails?

  • 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-12T11:44:49+00:00Added an answer on May 12, 2026 at 11:44 am

    You are right that the wrong association is being used here.

    In ActiveRecord, they model that has the foreign key always belongs_to another model.

    In this scenario class Foo actually belongs_to those props

    One way to specify this would be:

    class Foo < ActiveRecord::Base
     belongs_to :prop_a, :class_name => "Bar", :foreign_key => "prop_a_id"
     belongs_to :prop_b, :class_name => "Bar", :foreign_key => "prob_b_id"
     belongs_to :prop_c, :class_name => "Bar", :foreign_key => "prob_c_id"
    end
    

    What this means though, is that you must have a column on Foo titled “prop_a_id, prop_b_id, and prop_c_id” which can store the integer that is the primary key of the Bar table.

    This solution, however, does not take care of the problem listed below the ActiveRecord associations. For the solution Will proposed above, you would need to take a look at Rails and Single Table Inheritance. If you google this you can find many resources on it. Personally, I recommend Agile Web Development with Rails. In the 3rd edition you can find it on page 377. Also there is a good beginner write-up on STI here

    Good luck!

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

Sidebar

Ask A Question

Stats

  • Questions 175k
  • Answers 175k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would encode the bytes in base64, using System.Convert.ToBase64String and… May 12, 2026 at 3:02 pm
  • Editorial Team
    Editorial Team added an answer They write the file to a temp directory. You see… May 12, 2026 at 3:02 pm
  • Editorial Team
    Editorial Team added an answer The coordinates you're seeing are due to the fact that… May 12, 2026 at 3:02 pm

Related Questions

This is a part algorithm-logic question (how to do it), part implementation question (how
I'm fairly new to the OO design process, so please bear with me.... I
I have several classes that need to load some properties files, and I was
I am programming a simple game in Java but I am trying to do

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.