作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
在我的 Rails 4.2.7、ruby 2.3.3 应用程序中,当我通过保存嵌套表单来保存特定的多态关联时,它会给出堆栈级别太深的错误并且rails_admin 停止响应。当我从 Rails 控制台保存时,出现以下错误,fatal: exception reentered 错误。
2.3.3 :001 > Post.last.links
Post Load (10.3ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
Link Load (0.8ms) SELECT "links".* FROM "links" WHERE "links"."linked_item_id" = $1 AND "links"."linked_item_type" = $2 [["linked_item_id", 112], ["linked_item_type", "Post"]]
=> #<ActiveRecord::Associations::CollectionProxy []>
2.3.3 :002 > Post.last.links.create(name: 'see details', link: 'http://www.google.com/')
Post Load (3.7ms) SELECT "posts".* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1
(1.2ms) BEGIN
SQL (4.1ms) INSERT INTO "links" ("name", "link", "linked_item_id", "linked_item_type", "target", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [["name", "see details"], ["link", "http://www.google.com/"], ["linked_item_id", 112], ["linked_item_type", "Post"], ["target", "_blank"], ["created_at", "2017-04-11 15:34:25.320118"], ["updated_at", "2017-04-11 15:34:25.320118"]]
(0.3ms) ROLLBACK
fatal: exception reentered
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/transaction.rb:187:in `rescue in within_new_transaction'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/transaction.rb:201:in `within_new_transaction'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/transactions.rb:220:in `transaction'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:182:in `transaction'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:492:in `_create_record'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/associations/has_many_association.rb:187:in `_create_record'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_association.rb:153:in `create'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/activerecord-4.2.7.1/lib/active_record/associations/collection_proxy.rb:290:in `create'
from (irb):2
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/railties-4.2.7.1/lib/rails/commands/console.rb:110:in `start'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/railties-4.2.7.1/lib/rails/commands/console.rb:9:in `start'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:68:in `console'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/railties-4.2.7.1/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
from /Users/starwar/.rvm/gems/ruby-2.3.3@rails420/gems/railties-4.2.7.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
这是我的 Post 模型的相关部分
class Post < ActiveRecord::Base
has_many :links, -> { order(created_at: :asc) }, as: :linked_item, dependent: :destroy
accepts_nested_attributes_for :links, allow_destroy: true
还有我的链接模型
class Link < ActiveRecord::Base
extend Enumerize
enumerize :target, in: [:_blank, :_self, :_top, :_parent], default: :_blank
# Associations
belongs_to :linked_item, polymorphic: true, touch: true
# Validations
validates_presence_of :link
validates_presence_of :name
validates :link, :url => { allow_nil: false }
# Scopes
scope :sorted, -> { order(created_at: :asc) }
end
这个完全相同的代码部署在 Heroku 上,它工作正常,但在我的本地机器上,它不工作。当 rails 服务器停止响应时,我必须终止进程,它说
[NOTE]
You may have encountered a bug in the Ruby interpreter or extension libraries.
Bug reports are welcome.
For details: http://www.ruby-lang.org/bugreport.html
令人惊讶的是,我的测试通过了,但我无法通过 Rails 控制台或 rails_admin 面板向模型添加任何内容,该模型通过填充嵌套表单具有多态关联。
谢谢,
最佳答案
我已经解决了这个问题,我做了以下事情,但我不确定是哪一个解决了它。
以下是我认为可以解决此问题的步骤。
将 Rails 4.2.7.1 更新到 Rails 4.2.8。不过,我认为这并没有解决这个问题。
我正在使用 Searchkick gem 运行 elasticseach,我在进行更改后重新索引我的模型数据。
谢谢,
关于ruby-on-rails - 异常重新进入 : When saving polymorphic association,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350822/
我是一名优秀的程序员,十分优秀!