我尝试将我的ActiveRecord模型的一个实例序列化为JSON(使用“render:json => @entities”或“@ entities.to_json”),但我总是得到一个CircularReferenceError(“ActiveSupport :: JSON :: Encoding :: CircularReferenceError(对象引用自身)“)。 只有在使用ThinkingSphinx查询ActiveRecord实例时才会发生这种情况。 to_xml序列化工作正常。这是一个已知的问题吗?我使用的是最新的Rails 3和ThinkingSphinx。
答案 0 :(得分:2)
思考狮身人面像& Rails 3&在我使用以下内容之前,JSON无法在我的系统上运行:
@entities = Entity.find(:all)
render :js => @entities.as_json
答案 1 :(得分:1)
“render:json => @ entities.to_a”解决了这个问题。
答案 2 :(得分:0)
我最近也遇到了这个问题,然后我就像正在进行的那样改变它
render :json => @entities.to_a.to_json(:only => [:entities's attributes], :methods => [:entities's methods])
它有效。