PlankTon
PlankTon

Reputation: 12605

Rails 3 nested validations: Ignoring entry if all fields are blank?

I have a rails 3 form with nested attributes. So, for example, the parent "Shop" form has many nested "products" rows on the same form.

The "products" model has presence validations on it, but the problem is unless I fill out an entry for each product in the (nested) form, the presence validations fail.

So, in a nested form, how do I tell rails to ignore the entry if all fields for a given product are blank, but uphold presence validations if any field is filled out?

Many thanks

Upvotes: 1

Views: 1368

Answers (1)

Syed Aslam
Syed Aslam

Reputation: 8807

This should work for you:

accepts_nested_attributes_for :products, :reject_if => :all_blank, :allow_destroy => true

Upvotes: 1

Related Questions