【发布时间】:2011-04-13 00:27:01
【问题描述】:
我有一个多态关系,我希望孩子(多态?)是完全透明的。设置是通用的:
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。但是我该如何设置以便关系对ActiveRelation 也是透明的呢?让我做类似的事情:
AppointmentTypeOne.where('starts_at IS NOT NULL')
(不必join 或include :scheduled_event)
【问题讨论】:
标签: ruby-on-rails activerecord polymorphic-associations