title: Mongomapper/mongoDB: SystemStackError: stack level too deep
body: <p>I have a Rails 3 model "D" that belongs to another model, which belongs to another two in turn, like this:</p>
  
  <pre><code>D belongs to C
  C belongs to B
  B belongs to A
  </code></pre>
  
  <p>I am using MongoDB/Mongomapper as my ORM.</p>
  
  <p>When I try to instantiate D, I am getting an error:</p>
  
  <pre><code>ruby-1.9.2-preview3 &gt; d = D.new
  SystemStackError: stack level too deep
  from /Users/peter/.rvm/rubies/ruby-1.9.2-preview3/lib/ruby/1.9.1/irb/workspace.rb:80
  Maybe IRB bug!!
  </code></pre>
  
  <p>I don't have this problems instantiating and working with the other three models.</p>
  
  <p>Below are more details on my setup:</p>
  
  <p>Agency has many Properties, which has many Inspections, which 
  has many Items. In both the controller and on IRB I try to get the 
  items that belong to an inspection document/instance, and get the 
  "stack level too deep" error. </p>
  
  <pre><code>class Agency 
    include MongoMapper::Document 
    key :name, String 
    timestamps! 
    # Assocations ::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    many :properties 
  end 
  
  class Property 
    include MongoMapper::Document 
    timestamps! 
    # Assocations ::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    belongs_to :agency 
    many :inspections 
    key :address_postcode, String 
  end 
  
  class Inspection 
    include MongoMapper::Document 
    timestamps! 
    # Assocations ::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    belongs_to :property 
    many :items 
    key :date, Date 
  end 
  
  class Item 
    include MongoMapper::Document 
    timestamps! 
    # Assocations ::::::::::::::::::::::::::::::::::::::::::::::::::::: 
    belongs_to :inspection 
    key :area_comment, String 
  end 
  </code></pre>
  
  <p>In the controller: </p>
  
  <pre><code>def show 
          @inspection = Inspection.find_by_id(params[:id]) 
          @items = @inspection.items 
          puts "Inspection: #...@inspection.id}" 
          puts "Items: #{@items}" 
          respond_to do |format| 
              format.html # show.html.erb 
              format.json  { render :json =&gt; @items } 
          end 
  end 
  </code></pre>
  
  <p>Then, in IRB: </p>
  
  <pre><code>Loading development environment (Rails 3.0.0.beta4) 
  ruby-1.9.2-preview3 &gt; inspection = Inspection.find_by_id("4c4e183d55899f3d66000002") 
  =&gt; #&lt;Inspection _id: BSON::ObjectID('4c4e183d55899f3d66000002'), 
  created_at: Mon, 26 Jul 2010 23:20:37 UTC +00:00, updated_at: Mon, 26 
  Jul 2010 23:22:04 UTC +00:00, date: nil, property_id: BSON::ObjectID('4c4e181a55899f3d66000001')&gt; 
  ruby-1.9.2-preview3 &gt; puts inspection.items 
  SystemStackError: stack level too deep 
  from /Users/peter/.rvm/rubies/ruby-1.9.2-preview3/lib/ruby/1.9.1/irb/ 
  workspace.rb:80 
  Maybe IRB bug!! 
  </code></pre>
  
  <p>OR, in the browser: </p>
  
  <pre><code>URL: http://localhost:3000/items/4c4e183d55899f3d66000002 
  Returns: 
  500 Internal Server Error 
  If you are the administrator of this website, then please read this web application's log file and/or the web server's log file to find out what went wrong. 
  while in the log console: 
  Started GET "/items/4c4e183d55899f3d66000002" for 127.0.0.1 at 
  2010-07-28 10:53:57 +1000 
  Processing by ItemsController#show as HTML 
  Parameters: {"id"=&gt;"4c4e183d55899f3d66000002"} 
  Completed   in 24ms 
  SystemStackError (stack level too deep): 
  /Users/peter/.rvm/gems/ruby-1.9.2-preview3@rails300/gems/ 
  activesupport-3.0.0.beta4/lib/active_support/callbacks.rb:419 
  Rendered /Users/peter/.rvm/gems/ruby-1.9.2-preview3@rails300/gems/ 
  actionpack-3.0.0.beta4/lib/action_dispatch/middleware/templates/ 
  rescues/_trace.erb (1.2ms) 
  Rendered /Users/peter/.rvm/gems/ruby-1.9.2-preview3@rails300/gems/ 
  actionpack-3.0.0.beta4/lib/action_dispatch/middleware/templates/ 
  rescues/_request_and_response.erb (3.6ms) 
  Rendered /Users/peter/.rvm/gems/ruby-1.9.2-preview3@rails300/gems/ 
  actionpack-3.0.0.beta4/lib/action_dispatch/middleware/templates/ 
  rescues/diagnostics.erb within rescues/layout (45.7ms) 
  </code></pre>
  
  <p>Any ideas? What am I doing wrong? </p>
  
  <p>Thanks, 
  Peter </p>