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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:10:29+00:00 2026-06-03T22:10:29+00:00

I have class Question < ActiveRecord::Base has_many :tasks, :as => :task end class QuestionPlayer

  • 0

I have

class Question < ActiveRecord::Base
   has_many :tasks, :as => :task
end

class QuestionPlayer < Question
end

class QuestionGame < Question
end

class Tast < ActiveRecord::Base
  belongs_to :task, :polymorphic => true
end

when I do

Task.create :task => QuestionPlayer.new
#<Task id: 81, ... task_id: 92, task_type: "Question">

why? How can I get Task with task_type = “QuestionPlayer” ?

  • 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-06-03T22:10:30+00:00Added an answer on June 3, 2026 at 10:10 pm

    The reason is that you are not actually using polymorphism, you are using STI (Single Table Inheritance). You are defining and setting up both, but only using STI.

    The purpose of the foreign key, even a polymorphic foreign key, as you defined it, is to reference another record in a table in the database. Active record must store the primary key and the class which has the table name for the record. This is exactly what it is doing.

    Maybe what you really want to do is use STI with different classes for each of your Question objects. In that case, do this,

    class CreateQuestionsAndTasks < ActiveRecord::Migration
      def self.up
        create_table :questions do |table|
          table.string :type
        end
        create_table :tasks do |table|
          table.integer :question_id
        end
      end
    end
    class Question < ActiveRecord::Base
      has_many :tasks
    end
    class QuestionPlayer < Question
    end
    class QuestionGame < Question
    end
    class Task < ActiveRecord::Base
      belongs_to :question
    end
    

    Now it will work as you figured.

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

Sidebar

Related Questions

I have the following: class Person < ActiveRecord::Base has_many :tasks end class Task <
Say you have this: class Question < ActiveRecord::Base has_many :answers end class Answer <
Newbie question. I have the following models: class Asset < ActiveRecord::Base belongs_to :assetable, :polymorphic
I have a simple models class Item < ActiveRecord::Base has_many :parts end class Part
Let's say I have: class Foo < ActiveRecord::Base has_many :bar end class Bar <
I currently have the following: class Question < ActiveRecord::Base belongs_to :product belongs_to :user belongs_to
I have a has_many and belongs_to association. class Link < ActiveRecord::Base has_and_belongs_to_many :categories belongs_to
I have two models: class Member < ActiveRecord::Base has_many :member_tags end and class MemberTag
Referencing question #2013421, I have the following RoR models: class Game < ActiveRecord::Base has_many
I have class Car < ActiveRecord::Base; end class Porsche < Car has_many :page_items, :as=>:itemable,

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.