レール4にSTIを実装しようとしましたが、動作させることができません。多くの検索結果を検索しましたが、何も機能しませんでした。ここに問題があります:Rails has_many STI
私はSTIを使ってインスタンスクラスを持っています。サブクラスCar(ダミーサブクラス)とScheduledInstanceクラスがあります。
タスクの車やタスクのscheduledinstancesを取得しようと、それは動作しませんclass Instance < ActiveRecord::Base
belongs_to :task
end
class Car < Instance end
class ScheduledInstance < Instance end
class Task < ActiveRecord::Base
has_many :instances, dependent: :destroy
has_many :cars
has_many :scheduledinstances
end
。(私はインスタンスのテーブルの上に型の列を持っている)
Task.first.cars
Task Load (0.8ms) SELECT "tasks".* FROM "tasks" ORDER BY "tasks"."id" ASC LIMIT 1
NameError: uninitialized constant Task::Car
しかし、私はTask.firstをすれば.instances、そしてTask.first.carsを実行するとOKです。私は何が欠けているのですか?
回答に基づいて、has_many throughで動作させるためにはどのような変更を適用する必要がありますか?
class Project < ActiveRecord::Base
has_many :tasks, dependent: :destroy
has_many :instances, through: :tasks
end
私はbelongs_toの追加だけでタイトル –