以下问题:我正在生成我的数据库模型的 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
要生成我调用的 XMLEntity.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
。
我正在寻找的是一种算法,它查看当前属性是 aAttribute
还是 aRelationship
以及包含对应关系的 the 。