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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:03:29+00:00 2026-05-23T22:03:29+00:00

Let’s say I have a person table, and I want it to classify all

  • 0

Let’s say I have a person table, and I want it to classify all people as either alphas or omegas. All omegas have exactly one alpha but no omegas, all alphas have any number of omegas but no alpha.

This is a simple two level hierarchy, which I can encode using a single foreign key:

CREATE TABLE people (
  id INTEGER NOT NULL PRIMARY KEY,
  alpha_id INTEGER FOREIGN KEY REFERENCES people, 
  -- alpha_id is NULL if and only if this person is an alpha
  -- other stuff we know about people...
);

Now I can create a general person class, but it gets slightly awkward when I get to the alpha-omega relationship.

class Person < ActiveRecord::Base
  # ... stuff I know about people

  # if alpha_id is NULL
    has_many :omegas, :as => :alpha, :class_name => Person
  # else 
    belongs_to :alpha, :class_name => Person
end

It’d be nice to split the person off into two subclasses, one for Alphas, one for Omegas, but I’m not sure how well that’d play with ActiveRecord.

Ideally, I’d like something like this:

class Person < ActiveRecord::Base
  # ... stuff I know about people
end
class Alpha < Subset(Person)
  column_is_null :alpha_id
  has_many :omegas
end
class Omega < Subset(Person)
  column_is_not_null :alpha_id
  belongs_to :alpha
end

Is this sort of subclassing, or something approximating it, available in ActiveRecord?

  • 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-23T22:03:29+00:00Added an answer on May 23, 2026 at 10:03 pm

    Use named_scope:

     class Person < ActiveRecord::Base
       # ... stuff I know about people
       named_scope :alphas, :conditions => { :alpha_id => nil }
       named_scope :omegas, :conditions => "alpha_id IS NOT NULL"
    
       # if alpha_id is NULL
       has_many :omegas, :as => :alpha, :class_name => Person
       # else 
       belongs_to :alpha, :class_name => Person
     end
    

    Now you can reference Person.alphas and Person.omegas to get the objects you are looking for. Does this help?

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a table with a Color column. Color can have various
Let's say I have thousands of users and I want to make the passwords
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have a C++ Visual Studio 2010 solution with 2 projects: one
Let's say I have table with column 'URL' whrere I store urls like this
Let's say you have two different categories of users with different profiles: one for
Let's say I have a Products table, ProductsCategory table and a Category table. The
Let's say I have a drive such as C:\ , and I want to
Let's say I have a large amount of files, say 20GB worth, all encrypted

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.