我的问题如下:当我尝试在ActiveAdmin面板中更新模型(comment.rb
)时,出现Stack level too deep
错误。
更新任何其他模型在ActiveAdmin面板中工作得很好,当我删除或创建新的注释时,也没有错误。
所以,我尝试使用以下命令通过rails console
更新数据库中的注释记录:Comment.first.update_column :body, 'hello'
它工作正常,但每次我尝试更新ActiveAdmin面板中的记录时,我都会得到这个无限递归,说实话,我甚至无法弄清楚是什么导致它。
单击ActiveAdmin面板中的“更新”按钮时,以下是错误的堆栈跟踪部分:
Started PATCH "/admin/product_comments/2" for 127.0.0.1 at 2015-08-18 15:24:38 +0300
Processing by Admin::ProductCommentsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"AhytIgM3DisFl4QZJaPL2JxFOC/3wKOawpeg6TT/GK3OyVUeotZ4whiQydKe/Jf4vLTsbrGicuiIr3ZfuQUC3Q==", "comment"=>{"user_id"=>"801", "product_id"=>"817", "body"=>"hello world!"}, "commit"=>"Update Comment", "id"=>"2"}
AdminUser Load (0.2ms) SELECT "admin_users".* FROM "admin_users" WHERE "admin_users"."id" = $1 ORDER BY "admin_users"."id" ASC LIMIT 1 [["id", 1]]
Comment Load (0.2ms) SELECT "comments".* FROM "comments" WHERE "comments"."id" = $1 LIMIT 1 [["id", 2]]
(0.1ms) BEGIN
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 801]]
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'user801@example.com' AND "users"."id" != 801) LIMIT 1
Product Load (0.1ms) SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1 [["id", 817]]
Comment Load (0.1ms) SELECT "comments".* FROM "comments" WHERE "comments"."product_id" = $1 [["product_id", 817]]
CACHE (0.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 801]]
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'user801@example.com' AND "users"."id" != 801) LIMIT 1
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE ("users"."email" = 'user801@example.com' AND "users"."id" != 801) LIMIT 1
然后User Exists
行重复了几十亿次。
当User Exists
行结束时,会出现另一段stacktrace:
(0.1ms) ROLLBACK
Completed 500 Internal Server Error in 198ms (ActiveRecord: 33.7ms)
SystemStackError - stack level too deep:
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql/utils.rb:72:in `new'
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql/utils.rb:72:in `extract_schema_qualified_name'
activerecord (4.2.3) lib/active_record/connection_adapters/postgresql/quoting.rb:31:in `quote_table_name'
arel (6.0.2) lib/arel/visitors/to_sql.rb:770:in `quote_table_name'
arel (6.0.2) lib/arel/visitors/to_sql.rb:705:in `visit_Arel_Attributes_Attribute'
arel (6.0.2) lib/arel/visitors/reduce.rb:13:in `visit'
arel (6.0.2) lib/arel/visitors/to_sql.rb:669:in `visit_Arel_Nodes_Equality'
arel (6.0.2) lib/arel/visitors/reduce.rb:13:in `visit'
它也是大约十亿行。
以下是我的Comment
模型的样子:
class Comment < ActiveRecord::Base
belongs_to :user
belongs_to :product
validates :body, presence: true, length: { maximum: 500 }
validates :user_id, :product_id, presence: true, numericality:
{ greater_than: 0, only_integer: true }
validates_associated :user, :product
# Method for displaying comment's id, user's full
# name and product name in AdminPanel. ActiveAdmin
# uses the `display_name` method in models for its
# drop-down inputs.
def display_name
id.to_s + '. From: ' + user.first_name + ' ' +
user.last_name + '. On: ' + product.name
end
end
在admin/comment.rb
我有以下内容:
ActiveAdmin.register Comment, as: "ProductComment" do
# Yep, that's a lot of params, but I thought adding them would help :)
permit_params :id, :user_id, :product_id, :body, :user, :product
index do
selectable_column
id_column
column :user
column :product
column :body
actions
end
filter :user
filter :product
filter :created_at
form do |f|
f.inputs "Edit Product Comment" do
f.input :user
f.input :product
f.input :body
end
f.actions
end
end
您能否帮我弄清楚这里发生了什么以及如何解决这个问题?
PS:如果我没有提供足够的信息,这里有一些我repository的链接:
答案 0 :(得分:1)
validates_associated
表示您在保存评论记录时也会验证用户记录。
您对用户记录进行了验证,您在其中指定电子邮件必须是唯一的,但验证(或用户记录中的某些其他验证)存在缺陷...作为您尝试保存用户记录的验证的一部分,但是当您保存用户记录时,您正在调用验证,并且作为验证的一部分,您尝试保存用户记录...不会结束(直到您用完堆栈)。
请查看您的用户验证。要证明这是问题所在,您可以暂时删除validates_associated