Rails透明子关系

| 我有一个多态关系,我希望孩子(多态?)是完全透明的。该设置是通用的:
class ScheduledEvent < ActiveRecord::Base
    belongs_to :scheduleable, polymorphic:true
    #has column names like #starts_at, #ends_at
end
class AppointmentTypeOne < ActiveRecord::Base
    has_one :scheduled_event, :as=>:scheduleable, :dependent=>:destroy
end
class AppointmentTypeTwo < ActiveRecord::Base
    has_one :scheduled_event, :as=>:scheduleable, :dependent=>:destroy
end
我希望能够将
AppointmentTypeOne
AppointmentTypeTwo
当作它们具有
#starts_at
#ends_at
表列。 从方法上来说,很容易将
#starts_at
#starts_at=
等添加到我的
AppointmentX
类中,并返回到
ScheduledEvent
。但是,如何设置关系也对ѭ9透明?让我做类似的事情:
AppointmentTypeOne.where(\'starts_at IS NOT NULL\')
(不必to11ѭ或
include
13ѭ)     
已邀请:
听起来您想使用单表继承,而不是has_one关联。这样您就可以为每种约会类型创建
ScheduledEvent
的子类:
class ScheduledEvent < ActiveRecord::Base

end

class AppointmentTypeOne < ScheduledEvent

end

class AppointmentTypeTwo < ScheduledEvent

end
基本上,您将一个类型列添加到Scheduled_events表中,而Rails负责其余的工作。 该论坛帖子涵盖了所有详细信息:http://railsforum.com/viewtopic.php?id=3815     

要回复问题请先登录注册