The exception was reintroduced: while maintaining polymorphic association
In my Rails 4.2.7 , ruby 2.3.3 application, when I save a certain polymorphic association while keeping the nested form, it gives the stack level too high and rails_admin becomes unresponsive. And when I save the Rails console it gives the following error: fatal: Re-entry exception .
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>'
Here is an important part of my posting model
class Post < ActiveRecord::Base
has_many :links, -> { order(created_at: :asc) }, as: :linked_item, dependent: :destroy
accepts_nested_attributes_for :links, allow_destroy: true
And my link model
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
This exact code is deploying to Heroku and it works great, but it doesn't work on my local machine. And when the rails server stops responding, I have to kill the process and it says
[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
And suddenly my tests pass, but I can't add anything via the Rails console or rails_admin panel to a model that has a polymorphic association filling out a nested form.
Thank,
source to share
I solved this problem, I did the following things and I am not sure which one solved it.
- I used postgres 9.2 and updated to the latest 9.6. I don't think I solved this problem, I have to do it and I did it anyway because heroku is running 9.5, so I thought it was time for an update. I do not recommend that you do this.
Below are the steps that I believe solved this problem.
-
Updated Rails 4.2.7.1 to Rails 4.2.8. I don't think it solved this problem though.
-
I am doing elastic capture using Searchkick map, I re-indexed my model data after making changes.
- Updated OJ gem to the latest version (2.16.1) and Downgraded enumerize to a minor version (2.0.1) and updated Friendly_id . to the latest version (5.2.1). I think this solved this problem.
Thank,
source to share