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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:05:01+00:00 2026-05-23T05:05:01+00:00

I have a class heirarchy but not single-table inheritence. ThrowAway < ActiveRecord::Base and Junk

  • 0

I have a class heirarchy but not single-table inheritence.

ThrowAway < ActiveRecord::Base

and

Junk < ThrowAway

the problem is that all ThrowAway objects have references to a Location. A Location

belongs_to :throw_away, :polymprohic => true

The problem is that if I define

ThrowAway < ActiveRecord::Base
  has_many :locations, :as => :throw_away
end

then even if Junk inherits from it and defines a different table name, the throw_away_type column will always be set to ThrowAway where I actually want it set to Junk.

Now there will be many of these subclasses so there will be Stuff < ThrowAway Rags < ThrowAway etc. I want them all to have a Location without defining a location relationship in each individual class.

Is this possible with rails? Problem is that there is not just location, there are other of these sort of relationships and I’d rather follow some DRY here. I’m assuming I need to create a generator method which will execute on the current object and generate these relationships generators on runtime.

Thanks.

  • 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-23T05:05:02+00:00Added an answer on May 23, 2026 at 5:05 am

    ActiveRecord doesn’t seem to be able to cooperate with model inheritance that’s not STI. Fortunately, that’s probably not what you really want anyway. You probably want a mixin instead.

    Create a module that contains all of the functionality that you want your models to share in common, and have all of your models include that module. I would probably put the module in your models directory, but give it an adjective as a name. Some folks might put the module in the lib directory or create a lib directory within the models directory for it.

    In your case, you’ll want the association to be defined at the time the module is mixed in, so you’ll need to use a callback. Something like..

    module Trashable
      def self.included(base)
        base.send :has_many, :locations, :as => :throw_away
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have one main interface and an abstract class implementing all derivable methods (that
I have a simple Class Hierarchy that I am trying to get to work
Suppose we have the following class hierarchy: class Base { ... }; class Derived1
Let's say I have the following class hierarchy in C++: class Base; class Derived1
I have the following class hierarchy in C++: class Base { virtual void apply()
Let's say I have a person table, and I want it to classify all
I have a class that gets arranged into a hierarchy, such as this: class
I have a class hierarchy where __init__ in class Base performs some pre-initialization and
I have a class hierarchy that I want to map across several tables using
I have a mapping issue with the table-per-class hierarchy in Fluent/NHibernate. When retrieving the

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.