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ѭ)
没有找到相关结果
已邀请:
1 个回复
痰嫉
的子类:
基本上,您将一个类型列添加到Scheduled_events表中,而Rails负责其余的工作。 该论坛帖子涵盖了所有详细信息:http://railsforum.com/viewtopic.php?id=3815