следующая проблема: я генерирую 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)
в Entity self.includes
выглядит так:
def self.xml_includes
includes = {}
includes[:properties] = { :include => :enum, :include => :entities }
return includes
end
в настоящее время это работает, но кажется, что это просто обходной путь, потому что Property
должен иметь те же отношения, что и классы, наследуемые от Property
.
Я ищу алгоритмон определяет, является ли текущее свойство Attribute
или Relationship
и включает ли соответствующие соотношения.