New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rails bi-directional accepts_nested_attributes_for leads to unexpected validation result #41811
Comments
What happens if you set Also would be great if you can put your steps into a runnable repro script :) |
@intrip adding |
@rsalehi2013 thanks I'll take a look |
@rsalehi2013 #41878 fixes this issue: please check. Waiting for feedback from the core team now 😄 |
When defining bi-directional `accepts_nested_attributes` the validation of a collection doesn't work well: in particular only the last record of the collection is used. This happens because each child item in the collection calls `valid?` on the parent record erasing the previous validation errors stored in the parent. Fixes the issue by tracking the record under validation and skipping cyclic `valid?` calls. Fixes rails#41811
This issue has been automatically marked as stale because it has not been commented on for at least three months. |
Still relevant, PR attached. |
Hello, any news on a fix ? |
There is an attached PR waiting for review |
@intrip I've seen it, thank you. I was more asking if it would be merged sometime soon, my bad. |
Nevermind, my issue was different and project specific! |
Hi, this issue looks a lot like this Stack Overflow bug report, where calling The workaround indicated in the accepted answer is easy and I confirm that it works: load the has_many before updating them. Here would be a way to apply this to the situation described: # app/models/post.rb
class Post < ActiveRecord::Base
has_many :comments, inverse_of: :post
accepts_nested_attributes_for :comments
validates :title, presence: true
# WORKAROUND STARTS HERE
def comments_attributes(params)
# Preload comments to ensure validation state remains in memory
comments.load
super
end
end |
Steps to reproduce
script: https://gist.github.com/rsalehi2013/7cf9c458af9923bcd3e2f3053cefcc55
Setup
Migration:
post.rb
comment.rb
Procedure
post = Post.new(title: 'foo', comments_attributes: [{body: 'bar'}, {body: nil}, {body: 'baz'}])
post.valid?
post.errors
Expected behavior
valid? -> false
errors -> @messages={:"comments.body"=>["can't be blank"]}, @details={:"comments.body"=>[{:error=>:blank}]}
user is prevented from persisting to the database
Actual behavior
valid? -> true
errors -> @messages={}, @details={}
user is able to persist the invalid records to the database
NOTE: if the invalid comments attribute is at the end of the comments_attributes list, or if there is only one item in the comments_attributes list, the post is correctly marked as invalid
System configuration
Rails version: Rails 5.2.4.5
Ruby version: ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
The text was updated successfully, but these errors were encountered: