Mongomapper / mongoDB:SystemStackError:堆栈级别太深

时间:2010-07-23 08:50:02

标签: ruby mongomapper ruby-on-rails-3

我有一个属于另一个模型的Rails 3模型“D”,它依次属于另外两个模型,如下所示:

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

我使用MongoDB / Mongomapper作为我的ORM。

当我尝试实例化D时,我收到错误:

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!!

我没有这个问题实例化和使用其他三个模型。

以下是有关我的设置的更多详细信息:

代理商拥有许多属性,其中包含许多检查 有很多项目。在控制器和IRB上我试着得到 属于检查文档/实例的项目,并获取 “堆栈级别太深”错误。

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 

在控制器中:

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 => @items } 
        end 
end 

然后,在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!! 

或者,在浏览器中:

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) 

有什么想法吗?我究竟做错了什么?

谢谢, 彼得

2 个答案:

答案 0 :(得分:2)

问题出在Item模型上。它包含密钥“:keys”。也许“:keys”是保留的,但无论如何,只要我重命名它,问题就解决了。

答案 1 :(得分:2)

我在我的模型中使用:class作为模型属性,这恰好是一个保留的,更改属性名称已经完成了我的工作。