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” ?
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,
Now it will work as you figured.