So it definitely has to do with trying to JSON root_document in the
data_binding. When you call view.to_json, it encodes the view first
and then cascades down through the associations. When it gets to the
data_binding, it tries to to_json the root_document, sees that we are
still trying to json the view, and raises the CircularReferenceError.
Have a look at activesupport-3.0.0.beta4/lib/active_support/json/
encoding.rb, line 57. If you trace through, you'll see the view object
put in the @seen array first and then later on through the
data_binding on the root_document.
The way I got around this was to remove the root and parent documents
from the hash before encoding. In my case, I don't need them, as I am
just using the JSON for the UI.
class View
include MongoMapper::Document
key :name, String
timestamps!
has_many :components
def as_json(options = {})
remove_root_and_parent_elements(super(options))
end
private
def remove_root_and_parent_elements(json)
if json.is_a?(Array)
json.inject([]) { |arr, element| arr <<
remove_root_and_parent_elements(element) }
elsif json.is_a?(Hash)
cleaned_hash = {}
json.each do |key, value|
cleaned_hash[key] = remove_root_and_parent_elements(value)
unless (key == "_parent_document" || key == "_root_document")
end
cleaned_hash
else
json
end
end
end
It would seem that the right solution would be to modify mongo_mapper/
plugins/serialization.rb#as_json to not serialize root_documents if
include_root_in_json is false, unless I misunderstand what the intent
of that is.
Thoughts?
Thanks,
Scott
On Aug 19, 2:43 pm, hamin <
aminhar...@gmail.com> wrote:
> Yeah I was running into this problem yesterday too and ended up using
> @foo.attributes.to_json
>
> On Aug 19, 11:04 am, Scott Tamosunas <
tamosu...@gmail.com> wrote:
>
>
>
> > Cool, I'll give that a shot.
>
> > I have been investigating and I get the feeling it has to do with
> > _root_document being in the hash that get'sJSON'ed. The
> > _root_document is the View for the DataBinding object and we are still
> > descended into the View'sJSONencoding, thus the view is in the @seen