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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:18:18+00:00 2026-05-18T02:18:18+00:00

Even though I know that, at least to my knowledge, this is not the

  • 0

Even though I know that, at least to my knowledge, this is not the default way of doing associations with ActiveRecord I’m looking for help in order to implement an “hibernatish” Polymorphic model.

For instance, consider the following base model:

# Content only has one property named path
class Content < ActiveRecord::Base
  self.abstract_class = true
end

And the concrete content:

# Audio only has one property on it's own (duration).
# However, it should also inherit Content's property (path)
class Audio < Content
end

Now, something relatively interesting happens when using ActiveRecord, more accurately Rails 3 beta 3 ActiveRecord. If you set the abstract_class to false on the Content model and you execute the following:

Audio.create!(:path => '/dev/null')

It kinda works from an Hibernate perspective. That is, a Content record is created with ID 1 and an Audio record is also created with the ID = 1.

However, the problem #1 is that in order for this to work, you obviously need to turnoff the abstract_class, which kinda breaks the whole point of this abstract content example.

Furthermore, the problem #2 is that if you turn on the abstract_class you lose the content’s properties when creating instances of Audio. And, if you turn it off, you lose the Audio properties when creating an instance of Audio.

Ideally, when faced with an abstract class that is then subclassed, ActiveRecord would provide the abstract + concrete properties to the concrete class being instantiated, in this case Audio. Effectively, that way, when creating an instance of Audio we would have:

audio = Audio.new #=> <Audio id: nil, duration: nil, path: nil, created_at: nil, updated_at: nil> 

And then [naturally] when you assign audio.path = ‘/dev/null’ and performed a save operation, ActiveRecord would know that the path attribute has been inherited, thus needs to be persisted at the parent class level. Furthermore, on that same save operation, should you set a non inherited property of audio ActiveRecord would also persist those changes in the audios table.

My question, after this introduction, is how one could go around active record and enhance it that way?

Effectively, let’s assume that we’re developing a gem that aims to provide active record with this kind of functionality. How would you go about and do it?

PS: I’m actually considering to develop such gem, though such hackery shouldn’t go by without prior thinking. So, your feedback is most welcomed.

Best regards,
DBA

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

    I see what you’re going for here, but unfortunately I don’t think you can get this to work out of the box the way you’d like.

    Basically ActiveRecord uses model inheritance for two things:

    • Abstract classes, from which subclasses can inherit code and behavior but NOT table structure. You could implement a #path attribute on Content using attr_accessor and it would be inherited by subclasses, but it would not be persisted to the database unless the table for your model subclass had a ‘path’ column.

    • Single table inheritance, where subclasses inherit both behavior and table persistence.

    You can mix the two, but there is no scenario where an ActiveRecord model can inherit a persistable column from an abstract class. There’s always a 1:1 mapping between persisted attributes and table columns.

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

Sidebar

Related Questions

One of my co-workers claims that even though the execution path is cached, there
I saw that __VIEWSTATE field gets rendered even though I have set the EnableViewState=false
My application uses some session variables that are timing out even though I thought
First, I know this question has been asked several times before and that in
Even though I always strive for complete validation these days, I often wonder if
Even though I have a robust and fast computer (Pentium Dual Core 2.0 with
Even though I've been a developer for awhile I've been lucky enough to have
Even though MDI is considered harmful , several applications (even MS Office, Adobe apps)
I have code in an Update Panel and even though on a button click
How does container object like vector in stl get destroyed even though they are

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.