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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:50:44+00:00 2026-05-17T22:50:44+00:00

I have a Model called Todo.rb which has an attribute called asset which is

  • 0

I have a Model called Todo.rb which has an attribute called “asset” which is a string. The string contains the names of Models such as Call, Email, etc.

They also contain an asset_id, which is a specific record of the Asset in ActiveRecord.

How, from a Todo record, can I assign to asset from that actual ActiveRecord class?

=> #<Todo id: 165, created_at: "2010-11-04 00:49:45", updated_at: "2010-11-04 00:49:45", contact_id: 49, user_id: 1, asset: "Call", asset_id: 2, original_date: "2010-11-18", current_date: "2010-11-18", done_date: nil>

From this, I would like asset to be the equivalent of:

 asset = Call.find(2)

I understand conceptually this could be a case of single table inheritance, but I wasn’t clear how and I wanted to build it up from the use-case.

  • 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-17T22:50:44+00:00Added an answer on May 17, 2026 at 10:50 pm

    I understand what you’re trying to do here, and there’s a much simpler way to do it. It’s called polymorphism. You want todo’s to be able to belong to more than one type of thing – sometimes a call, sometimes email, and so on. The good news is, you’re pretty close already. Here’s how you set it up.

    In your migration, rename the “asset” field to “asset_type”:

    rename_column :todos, :asset, :asset_type
    

    Now add this to your models:

    class Todo < ActiveRecord::Base
      belongs_to :asset, :polymorphic => true
    end
    
    class Call < ActiveRecord::Base
      has_many :todos, :as => :asset
    end
    
    class Email < ActiveRecord::Base
      has_many :todos, :as => :asset
    end
    

    In the class that is going to belong_to many different types of objects, you have an asset_type string column, and an asset_id integer column. And you specify that it belongs to the polymorphic association, as shown above. Then, in all the models that can have_many todo’s, you add the :as => :asset part to tell it Rails how to find the associated todo’s.

    This is very easy, and now it works just like any other has_many. I hope this helps!

    UPDATE: This is an article I wrote that illustrates polymorphism not just at the model level, but also throughout your site’s forms:

    http://kconrails.com/2010/10/19/common-addresses-using-polymorphism-and-nested-attributes-in-rails/

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

Sidebar

Related Questions

In my user model, I have an attribute called nickname and validates as such:
Let's say I have a model called Theme, which has several attributes setting interface
I have a business model called Customer which has many required properties (via DataAnnotations)
Say I have a model object 'Person' defined, which has a field called 'Name'.
I have a model which has a field called deleted , which is used
Let's assume I have a model called product. Let's assume that product has three
I have a Thread model which has a show page, and in that action's
I have a model called Question and another one called Answer. class Question(models.Model): text
I have a model called NoteCategory, which is functioning as a join table between
I have a model, ContactEmail that has a date_sent attribute and an email_id, referencing

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.