Rails 7.1: implementing conditional validations in nested forms leading to advanced patterns
After trying multiple solutions online, I still can't figure this out. I'm experiencing unexpected behavior with conditional validations in my Rails 7.1 application when using nested forms. I have a `Project` model that has many `Tasks`, and I want to validate the presence of a `due_date` only if the `status` is set to 'active'. Here’s the relevant part of my `Project` model: ```ruby class Project < ApplicationRecord has_many :tasks, dependent: :destroy accepts_nested_attributes_for :tasks validates :name, presence: true validate :validate_tasks private def validate_tasks tasks.each do |task| if task.status == 'active' && task.due_date.blank? task.errors.add(:due_date, 'must be present if status is active') end end end end ``` And here’s how I’m rendering the nested form in my view: ```erb <%= form_with model: @project do |f| %> <%= f.text_field :name %> <%= f.fields_for :tasks do |task_form| %> <%= task_form.select :status, ['active', 'completed'] %> <%= task_form.date_select :due_date %> <% end %> <%= f.submit 'Save' %> <% end %> ``` When I try to submit the form with a task set to 'active' but without a `due_date`, I expect to see an behavior message for that task. Instead, I’m getting a validation behavior only on the `Project` level and not on the `Task` level, which is making it confusing for the user. I’ve tried explicitly calling `task.valid?` within the `validate_tasks` method, but that didn’t change the behavior. Additionally, when I check `task.errors.full_messages`, it’s empty right before the validation fails on the `Project`. Am I missing something in how I should be structuring the validations? Is there a better approach to ensure that the tasks are validated properly based on their status? Any insights would be greatly appreciated! This is part of a larger application I'm building. Thanks in advance!