Note that there are some explanatory texts on larger screens.

plurals
  1. PORails : JSON serialization circular reference error with Mongoid:Geospatial objects
    primarykey
    data
    text
    <p>I have an array of Mongo objects returned by a geolocation query such as :</p> <pre><code>@data = Record.geo_near([lng,lat], :max_distance =&gt; dist, :unit =&gt; :m, :spherical =&gt; true) </code></pre> <p>I'm then trying to serialize the response based on the expected format :</p> <pre><code>respond_to do |format| format.html format.json { render json: @data, :status =&gt; 200 } # Not working format.xml { render xml: @data, :status =&gt; 200 } # Working ! end </code></pre> <p>The weird thing is that everything go smoothly with XML, but I get this error with JSON :</p> <pre><code>ActiveSupport::JSON::Encoding::CircularReferenceError in BouncesController#populars object references itself </code></pre> <p>I found <a href="https://stackoverflow.com/questions/867491/geokit-rails-plugin-cant-call-to-json-on-returned-geoloc-object-gives-circul">this post</a> relating to the same kind of error, but the validated answer did not work for me.</p> <p>Any ideas?</p> <p><strong>EDIT</strong></p> <p>Here is my model for which the issue is happening :</p> <pre><code>class MyModel include Mongoid::Document include Mongoid::Timestamps include Mongoid::Spacial::Document spacial_index :loc, :min =&gt; -180, :max =&gt; 180 belongs_to :user field :text, :type =&gt; String field :loc, :type =&gt; Array, spacial: true field :accuracy, :type =&gt; Float def as_json(options={}) { "id" =&gt; self.id, "text" =&gt; self.text, "loc" =&gt; self.loc, "accuracy" =&gt; self.accuracy, "user" =&gt; { "id" =&gt; self.user['_id'], "login" =&gt; self.user['login'], "role" =&gt; self.user['role'] }, "created_at" =&gt; self.created_at, "updated_at" =&gt; self.updated_at } end end </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload