I have an array of Mongo objects returned by a geolocation query such as :
@data = Record.geo_near([lng,lat], :max_distance => dist, :unit => :m, :spherical => true)
I’m then trying to serialize the response based on the expected format :
respond_to do |format|
format.html
format.json { render json: @data, :status => 200 } # Not working
format.xml { render xml: @data, :status => 200 } # Working !
end
The weird thing is that everything go smoothly with XML, but I get this error with JSON :
ActiveSupport::JSON::Encoding::CircularReferenceError in BouncesController#populars
object references itself
I found this post relating to the same kind of error, but the validated answer did not work for me.
Any ideas?
EDIT
Here is my model for which the issue is happening :
class MyModel
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Spacial::Document
spacial_index :loc, :min => -180, :max => 180
belongs_to :user
field :text, :type => String
field :loc, :type => Array, spacial: true
field :accuracy, :type => Float
def as_json(options={})
{
"id" => self.id,
"text" => self.text,
"loc" => self.loc,
"accuracy" => self.accuracy,
"user" => {
"id" => self.user['_id'],
"login" => self.user['login'],
"role" => self.user['role']
},
"created_at" => self.created_at,
"updated_at" => self.updated_at
}
end
end
For now I have resolved the issue by downgrading my gems as follow :
There is still the possibility to use the following hack, but it prevents you to format yourself the json output in your model.