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

发布于 2024-09-11 06:17:02 字数 3732 浏览 3 评论 0原文

我有一个 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: #[email protected]}" 
        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) 

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

谢谢, 彼得

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千柳 2024-09-18 06:17:02

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

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.

带刺的爱情 2024-09-18 06:17:02

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

I was using :class as model attribute in my model which was happened to be a reserved, change that attribute name had done the job for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文