以下问题: 我正在生成我的数据库模型的xml文档。通过STI继承。
我的模特:
class Entity < ActiveRecord::Base
has_many :properties
belongs_to :relationship
end
class Property < ActiveRecord::Base
include Extensions::UUID
belongs_to :entity, :foreign_key => 'property_uid'
#just there for xml-creation
has_one :enum, :foreign_key => 'attribute_uid'
has_many :entities, :foreign_key => 'relationship_uid'
end
class Attribute < Property
has_one :enum, :foreign_key => 'attribute_uid'
end
class Relationship < Property
has_many :entities, :foreign_key => 'relationship_uid'
end
要生成XML,我调用Entity.to_xml(:skip_instruct => true, :include => Entity.xml_includes)
在实体中,self.includes
看起来像这样:
def self.xml_includes
includes = {}
includes[:properties] = { :include => :enum, :include => :entities }
return includes
end
这当前有效,但似乎只是一种解决方法,因为Property
必须与从Property
继承的类具有相同的关系。
我正在寻找的是一种算法,用于查看当前属性是Attribute
还是Relationship
,并包含相应的关系。