@entities\" or \"@entities.to_json\"), but I always get an CircularReferenceError (\"ActiveSup" />
开发者

CircularReferenceError when serializing an ActiveRecord instance (fetched by using ThinkingSphinx) to JSON

开发者 https://www.devze.com 2023-01-18 03:02 出处:网络
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 (\"ActiveSup

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.

0

精彩评论

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