开发者

Retrieving Rails 3 model with XML column

开发者 https://www.devze.com 2023-02-15 04:30 出处:网络
I have a Rails 3 model that includes a XML column in the database (IBM DB2). Whenever I try to retrieve this model in the XML format by @model.to_xml, I get as result the XML column escaped, something

I have a Rails 3 model that includes a XML column in the database (IBM DB2). Whenever I try to retrieve this model in the XML format by @model.to_xml, I get as result the XML column escaped, something like this:

<model>
    <id>1</id>
    <xml-column>&lt;tag&gt;value&lt;/tag&gt;</xml-column>
</model>

What I'm trying to achieve is the following:

<model>
    <id>1</id>
    <xml-column>
     开发者_StackOverflow社区   <tag>value</tag>
    </xml-column>
</model>

I tried some stuff unsuccessfully so far, like unescaping the XML column and rewriting the to_xml method (I'm not sure how to efficiently parse the XML column).

Any ideas?


That solved:

def to_xml(options = {})
  options[:indent] ||= 2
  xml = options[:builder] ||= Builder::XmlMarkup.new(:indent => options[:indent])
  xml.instruct! unless options[:skip_instruct]

  xml.model do
    xml.id self.id    
    xml.metadata do
      xml.target! << self.metadata
    end
  end
end

:)


Could you do something like:

def to_xml
   super(:except => [:column-xml]).merge!({:column-xml => self.column-xml})
end

I haven't tried this FYI

0

精彩评论

暂无评论...
验证码 换一张
取 消