Rails: 栈层级太深

8

实际上我正在做一个关于多对多模型的项目。我需要查找当前用户所有的积分/设备/项目(我认为只有两列(id和得分)的积分表是没有用的,所以我将其与连接表合并)。

我遇到了这个错误:

SystemStackError in Users#show

Showing app/views/shared/_credit.html.erb where line # raised:

stack level too deep

还有两个模型:

class **Credit** < ActiveRecord::Base
  attr_accessible :created_at, :credit_id, :device_id, :project_id, :score, :user_id

belongs_to :device
belongs_to :user
belongs_to :project
belongs_to :score

end

class **User** < ActiveRecord::Base

has_many :credit
has_many :credit, :through => :credit, foreign_key: "user_id", dependent: :destroy
end

感谢!祝一切顺利。

1
你是否了解has_and_belongs_to_many关系?请查看http://guides.rubyonrails.org/association_basics.html#the-has_and_belongs_to_many-association。另外,当你使用`has_many`时,应该使用`credits`而不是`credit`。 - Kulbir Saini
你定义了2个 has_many :credit;我认为第二个应该是 projects - Baldrick
1个回答

16

堆栈级别过深指的是无限递归调用,我想你是这样获得这个错误的:

has_many :credit, :through => :credit,

这明显展现了某种循环。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接