This project is archived and is in readonly mode.
:autosave => true + accepts_nested_attributes_for = circular validation & blown stack
Reported by Sai Emrys | December 16th, 2009 @ 09:51 PM
Consider the following:
class Discussion < ActiveRecord::Base
belongs_to :context, :polymorphic => true
has_many :messages, :dependent => :destroy, :inverse => :discussion
accepts_nested_attributes_for :messages
end
class Message < ActiveRecord::Base
belongs_to :discussion, :inverse => :messages, :autosave => true
validates_presence_of :discussion
def before_validation_on_create
self.discussion ||= Discussion.new
end
end
Scenario 1:
Create a new message. Save the message. Discussion gets created
pre-validation, the discussion gets validated and everything is
saved properly. All is well, huzzah.
Scenario 2:
Create a new discussion together with nested message attributes.
Save the discussion. It attempts to validate its messages, but they
in turn try to validate their discussion, which tries to validate
its messages... it recurses and blows stack. Boo.
This scenario can be prevented by turning off autosave => true, at the cost of breaking scenario 1.
This can also be prevented by turning off inverse (running from http://github.com/Fingertips/rails), at the cost of bug #2815 (#1943 may also be relevant), which makes Message's validation of its parent's existence fail. Additionally @discussion.messages.first.discussion != @discussion, which causes other issues. Fingertips' inverse_of patches nicely solve this.
I'm not sure how to fix this. Perhaps one could flag a record as having been validated at the BEGINNING of its validation cycle, and then not try to validate it again if that flag is set? This would prevent recursion, but might cause issues with mutually dependent validations of some sort that I'm failing to imagine.
Comments and changes to this ticket
-
Manfred Stienstra December 16th, 2009 @ 10:08 PM
- Title changed from :autosave => true + accepts_nested_attributes_for = circular validation & blown stack to :autosave => true + accepts_nested_attributes_for = circular validation & blown stack
- Assigned user set to Eloy Duran
Eloy, would you mind looking at this?
-
Sai Emrys December 16th, 2009 @ 10:36 PM
http://dev.rubyonrails.org/ticket/9355 may also be relevant.
-
Eloy Duran December 17th, 2009 @ 10:44 AM
I’m actually pretty glad you run into this problem, as you could verify the patch on #3533 for me :)
Please report on that ticket if the patch fixes your problem as expected, thanks.
-
Eloy Duran December 17th, 2009 @ 02:41 PM
Actually, instead of only that patch, could you try this Rails branch? http://github.com/Fingertips/rails/tree/2-3-stable
It contains the aforementioned patch, plus a few other related ones that I'd also love to be tested by others.
-
Eloy Duran December 28th, 2009 @ 08:53 PM
- State changed from new to resolved
The :inverse_of patches have been pushed to 2-3-stable, closing for now.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Referenced by
- 2815 nested models: build should directly assign the parent Bug #3588 is related to this - :autosave => true + accept...