Skip to content
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

Fix: bi-direction accepts_nested_attributes_for validation. #51795

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

akhilgkrishnan
Copy link
Member

Motivation / Background

Fixes #41811

Addresses the issue with bi-directional accepts_nested_attributes where validation of a collection fails due to only the last record being considered. This occurs because each child item triggers a valid? call on the parent record, which overwrites previous validation errors.

class Post < ActiveRecord::Base
  has_many :comments, inverse_of: :post
  accepts_nested_attributes_for :comments

  validates :title, presence: true
end

class Comment < ActiveRecord::Base
  belongs_to :post, inverse_of: :comments
  accepts_nested_attributes_for :post

  validates :body, presence: true
end

class BugTest < Minitest::Test
  # currently fails
  def test_invalid_attributes
    post = Post.new(title: "foo", comments_attributes: [{ body: "body 1" }, { body: nil }, { body: "body 3" }])

    refute post.valid?
    refute post.errors.empty?
  end

  # currently passes
  def test_invalid_attributes_end_of_list
    post = Post.new(title: "foo", comments_attributes: [{ body: "bar" }, { body: "baz" }, { body: nil }])

    refute post.valid?
    refute post.errors.empty?
  end

  # currently passes
  def test_invalid_attributes_one_attribute
    post = Post.new(title: "foo", comments_attributes: [{ body: nil }])

    refute post.valid?
    refute post.errors.empty?
  end
end

Detail

This PR is followup of #41878 which done by @intrip in 2 years back, additionally I've a added minor change for initialising the variable. This fix involves tracking the current record under validation and avoiding cyclic valid? calls.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

cc/ @byroot

@akhilgkrishnan akhilgkrishnan force-pushed the fix-bidirectional-nested-attribute branch from 215229e to 8b84d42 Compare May 12, 2024 11:58
Co-authored-by: Jacopo Beschi <intrip@users.noreply.github.com>
@akhilgkrishnan akhilgkrishnan force-pushed the fix-bidirectional-nested-attribute branch from 8b84d42 to 8fbbf10 Compare May 18, 2024 15:45
@rails-bot rails-bot bot added the docs label May 18, 2024
@akhilgkrishnan akhilgkrishnan requested a review from zzak May 18, 2024 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rails bi-directional accepts_nested_attributes_for leads to unexpected validation result
2 participants