# Validations on parent and accepts\_nested\_attributes\_for don't comply?

**URL:** <https://www.ruby-forum.com/t/validations-on-parent-and-accepts-nested-attributes-for-dont-comply/162502>\
**Category:** Rails\
**Created:** [March 10, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/validations-on-parent-and-accepts-nested-attributes-for-dont-comply/162502 "2009-03-10T16:07:40Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ronald\_Wind](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/b5a626/32.png) [@Ronald\_Wind](https://www.ruby-forum.com/u/Ronald_Wind)\
**Post date:** [March 10, 2009, 4:07pm UTC](https://www.ruby-forum.com/t/validations-on-parent-and-accepts-nested-attributes-for-dont-comply/162502/1 "2009-03-10T16:07:40Z")

</div>

Hello,

I’ve got two models:

class ProductGroup \< ActiveRecord::Base  
has\_many :fields, :dependent =\> :destroy  
accepts\_nested\_attributes\_for :fields  
end

class Field \< ActiveRecord::Base  
belongs\_to :product\_group  
validates\_presence\_of :product\_group  
end

Now when I try something like this:  
ProductGroup.create(:title =\> “foo”, :fields\_attributes =\> {“new\_1” =\>  
{:title =\> “bar”}})  
it’ll fail because of the validates\_presence\_of :product\_group  
validation. Since the field is being build by the  
product\_group.fields.build function (in  
assign\_nested\_attributes\_for\_collection\_association of the  
NestedAttributes module) the product reference of field is not set  
because the ProductGroup instance is not saved yet, and hence the  
validation fails.

Now if I remove the validation all goes well and both objects are saved  
as they should.

Is there a better / other way to make sure a Field can only be saved  
when it has a reference to a ProductGroup while still being able to use  
the nested attributes functionality?

Thanks,

Ronald
