Empact / roxml

ROXML is a module for binding Ruby classes to XML. It supports custom mapping and bidirectional marshalling between Ruby and XML using annotation-style class methods, via Nokogiri or LibXML.
http://roxml.rubyforge.org/
MIT License
224 stars 178 forks source link

Mapping ActiveRecords to non-AR ruby objects => SystemStackError #20

Open findchris opened 14 years ago

findchris commented 14 years ago

Hi there.

I have two (Rails) applications: One is using ActiveRecord, the other is not.

In the DB-backed app I have models like:

class Post < ActiveRecord::Base
  has_many :comments

  include ROXML
  xml_attr :id
  xml_attr :body
  xml_attr :comments, :as => [Comment], :in => "comments"
end

class Comment < ActiveRecord::Base
  belongs_to :post

  include ROXML
  xml_attr :id
  xml_attr :body
  xml_attr :post, :as => Post
end 

In the non-AR-based app, which will consume the XML generated from the AR-based app, I have:

class Post
  include ROXML

  xml_accessor :id
  xml_accessor :body
  xml_accessor :comments, :as => [Comment]
end
class Comment
  include ROXML

  xml_accessor :id
  xml_accessor :body
  xml_accessor :post, :as => Post
end

The problem is, when I try to generate the XML, I (not so unexpectedly) get this:

>> Post.first.to_xml.to_s
SystemStackError: stack level too deep

So the question is: How to map ActiveRecord objects (with associations) to non-ActiveRecord objects using ROXML?

In the app not using ActiveRecord, I'd like to be able to go through relations, such as in:

the_post.comments.post.name

Not sure if this is possible, but I'd appreciate your insight.

Cheers. -Chris

findchris commented 14 years ago

Forget the SystemStackError: The circular reference is probably not the way to go.

However, another question: ROXML overrides ActiveRecord's to_xml method and I was wondering if there was a way to get the XML that would be generated by ActiveRecord's to_xml() instead of ROXML's to_xml()?

Cheers