CircularReferenceError when serializing an ActiveRecord instance (fetched by using ThinkingSphinx) to JSON
I try to serialize an instance of my ActiveRec开发者_如何学JAVAord model to JSON (using "render :json => @entities" or "@entities.to_json"), but I always get an CircularReferenceError ("ActiveSupport::JSON::Encoding::CircularReferenceError (object references itself)"). This only happens when the ActiveRecord instances are queried by using ThinkingSphinx. The to_xml serialization works fine. Is that a known problem? I am using the latest Rails 3 and ThinkingSphinx.
Thinking Sphinx & Rails 3 & JSON didn't work on my system until I used the following:
@entities = Entity.find(:all)
render :js => @entities.as_json
"render :json => @entities.to_a" works around that problem.
I also get this problem recently, then I change it like the under way
render :json => @entities.to_a.to_json(:only => [:entities's attributes], :methods => [:entities's methods])
and it works.
精彩评论