0

大家好,我有一个多态关联,目前正在与几个不同的模型一起使用。每个模型都有与之关联的自己的视频文件,因此它使用“可视频”多态关联。然而,我最近发现需要创建一个具有 2 种不同类型视频的新模型。我会让代码说话。

#current setup
class Video < ActiveRecord::Base
   belongs_to :videoable, :polymorphic => true
end


class Project < ActiveRecord::Base
    has_one :video, :as => videoable
end

# New model I am working on
class Assignment < ActiveRecord::Base
    has_one :video_draft
    has_one :video_final
end

理想情况下,分配模型将具有两种特殊类型的视频对象,同时仍使用多态关联。我考虑过单表继承,但我不确定这是最好的方法。我有哪些选择?我不想创建 video_draft 模型和 video_final 模型,因为到最后,它们只是像其他所有内容一样的视频附件。唯一的区别是它们是专门的视频附件,需要自己独特的参考。

4

1 回答 1

0

您需要做的是ActiveRecord在声明时告诉您所指的模型:video_draft而不是模型中的:videolike Project。这可以通过将:class_name选项传递给has_one

记住这一点,这应该可以正常工作:

class Assignment < ActiveRecord::Base
    has_one :video_draft, :as => :videoable, :class_name => "Video"
    has_one :video_final, :as => :videoable, :class_name => "Video"
end

有关需要通过的更多信息,请参阅第 4.2.2.3 节的导轨指南:class_name

于 2011-02-12T16:17:57.120 回答