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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:31:17+00:00 2026-05-11T13:31:17+00:00

I think my question is best described as an example. Let’s say I have

  • 0

I think my question is best described as an example. Let’s say I have a simple model called ‘Thing’ and it has a few attributes that are simple data types. Something like…

Thing    - foo:string    - goo:string    - bar:int 

That isn’t hard. The db table will contain three columns with those three attributes and I can access them with something like @thing.foo or @thing.bar.

But the problem I’m trying to solve is what happens when ‘foo’ or ‘goo’ can no longer be contained in a simple data type? Assume that foo and goo represent the same type of object. That is, they are both instances of ‘Whazit’ just with different data. So now Thing might look like this…

Thing   - bar:int 

But now there is a new model called ‘Whazit’ that looks like this…

Whazit   - content:string   - value:int   - thing_id:int 

So far this is all good. Now here is where I’m stuck. If I have @thing, how can I set it up to refer to my 2 instances of Whazit by name (For the record, the ‘business rule’ is that any Thing will always have exactly 2 Whazits)? That is, I need to know if the Whazit I have is basically foo or goo. Obviously, I can’t do @thing.foo in the current setup, but I’d that is ideal.

My initial thought is to add a ‘name’ attribute to Whazit so I can get the Whatzits associated with my @thing and then choose the Whazit I want by name that way. That seems ugly though.

Is there a better way?

  • 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. 2026-05-11T13:31:17+00:00Added an answer on May 11, 2026 at 1:31 pm

    There are a couple of ways you could do this. First, you could set up two belongs_to/has_one relationships:

    things   - bar:int   - foo_id:int   - goo_id:int  whazits   - content:string   - value:int  class Thing < ActiveRecord::Base   belongs_to :foo, :class_name => 'whazit'   belongs_to :goo, :class_name => 'whazit' end  class Whazit < ActiveRecord::Base   has_one :foo_owner, class_name => 'thing', foreign_key => 'foo_id'   has_one :goo_owner, class_name => 'thing', foreign_key => 'goo_id'    # Perhaps some before save logic to make sure that either foo_owner   # or goo_owner are non-nil, but not both. end 

    Another option which is a little cleaner, but also more of a pain when dealing with plugins, etc., is single-table inheritance. In this case you have two classes, Foo and Goo, but they’re both kept in the whazits table with a type column that distinguishes them.

    things   - bar:int  whazits   - content:string   - value:int   - thing_id:int   - type:string  class Thing < ActiveRecord::Base   belongs_to :foo   belongs_to :goo end  class Whazit < ActiveRecord::Base   # .. whatever methods they have in common .. end  class Foo < Whazit   has_one :thing end  class Goo < Whazit   has_one :thing end 

    In both cases you can do things like @thing.foo and @thing.goo. With the first method, you’d need to do things like:

    @thing.foo = Whazit.new 

    whereas with the second method you can do things like:

    @thing.foo = Foo.new 

    STI has its own set of problems, though, especially if you’re using older plugins and gems. Usually it’s an issue with the code calling @object.class when what they really want is @object.base_class. It’s easy enough to patch when necessary.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The first way that comes to mind is to do… May 11, 2026 at 2:56 pm
  • added an answer Basically, python code, for the moment, will be divided into… May 11, 2026 at 2:56 pm
  • added an answer I'm guessing that you want to run your app from… May 11, 2026 at 2:56 pm

Related Questions

I think my question is best described as an example. Let's say I have
I'm trying to understand how grep works in this example. The code works but
I’m having a little Architecture problem. In my project I have a Business Logic
This is a broad-based question around delivering a javascript library that other web developers
I've been thinking a lot lately about a music-oriented project I'd like to work

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.