重新输入异常:保存多态关联时

时间:2017-04-11 15:54:29

标签: ruby-on-rails ruby postgresql activerecord callback

在我的 Rails 4.2.7 ruby​​ 2.3.3 应用程序中,当我通过保存嵌套表单保存特定的多态关联时,它会给出堆栈级别太深的错误和 rails_admin 停止响应。当我从Rails控制台保存时,它会出现以下错误,致命:异常重新输入错误。

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面板添加到具有多态关联的模型中。

谢谢,

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,我做了以下事情,我不知道其中哪一个解决了这个问题。

  • 我使用的是postgres 9.2并将其更新到最新的9.6。我不认为解决了这个问题,我应该这样做,无论如何我都这样做了,因为heroku运行9.5,所以我觉得是时候升级了。我不建议你这样做。

贝娄是我认为解决这个问题的步骤。

  • 将Rails 4.2.7.1更新为Rails 4.2.8。我不认为这解决了这个问题。

  • 我使用Searchkick gem运行elasticseach,在进行更改后重新编制索引我的模型数据。

  • OJ gem更新为最新版(2.16.1)并将枚举降级为次要版本(2.0.1)并更新 Friendly_id gem到最新版本(5.2.1)。我认为这解决了这个问题。

谢谢,