views:

114

answers:

1

I have a Rails 3 model "D" that belongs to another model, which belongs to another two in turn, like this:

D belongs to C
C belongs to B
B belongs to A

I am using MongoDB/Mongomapper as my ORM.

When I try to instantiate D, I am getting an error:

ruby-1.9.2-preview3 > 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!!

I don't have this problems instantiating and working with the other three models.

Below are more details on my setup:

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.

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 

In the controller:

def show 
        @inspection = Inspection.find_by_id(params[:id]) 
        @items = @inspection.items 
        puts "Inspection: #[email protected]}" 
        puts "Items: #{@items}" 
        respond_to do |format| 
            format.html # show.html.erb 
            format.json  { render :json => @items } 
        end 
end 

Then, in IRB:

Loading development environment (Rails 3.0.0.beta4) 
ruby-1.9.2-preview3 > inspection = Inspection.find_by_id("4c4e183d55899f3d66000002") 
=> #<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')> 
ruby-1.9.2-preview3 > 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!! 

OR, in the browser:

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"=>"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) 

Any ideas? What am I doing wrong?

Thanks, Peter

A: 

The problem was with the Item model. It contained the key ":keys". Perhaps ":keys" is reserved, but in any case, as soon as I renamed it, problem was solved.

futureshocked