在to_xml中包含子类

时间:2011-07-26 13:05:30

标签: ruby-on-rails xml inheritance sti

以下问题: 我正在生成我的数据库模型的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,并包含相应的关系。

0 个答案:

没有答案