# Accepts\_nested\_attributes\_for :reject\_if issue

**URL:** <https://www.ruby-forum.com/t/accepts-nested-attributes-for-reject-if-issue/170964>\
**Category:** Rails\
**Created:** [June 24, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-reject-if-issue/170964 "2009-06-24T15:39:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![andiug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@andiug](https://www.ruby-forum.com/u/andiug)\
**Post date:** [June 24, 2009, 3:39pm UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-reject-if-issue/170964/1 "2009-06-24T15:39:14Z")

</div>

I have the following models:

class Order \< ActiveRecord::Base

belongs\_to :billing\_address  
belongs\_to :shipping\_address

accepts\_nested\_attributes\_for :billing\_address  
accepts\_nested\_attributes\_for :shipping\_address, :reject\_if =\> proc {  
|attributes| attributes[‘has\_shipping\_address’] != ‘1’ }

def after\_initialize  
self.build\_billing\_address unless billing\_address  
self.build\_shipping\_address unless shipping\_address  
end

end

class ShippingAddress \< OrderAddress  
attr\_accessor :has\_shipping\_address  
end

class OrderAddress \< ActiveRecord::Base  
validates\_presence\_of :name  
#more validations here…  
end

And the view:

\<% form\_for @order do |f| %\>  
#…  
\<% f.fields\_for :shipping\_address do |addr\_f| %\>  
\<%= addr\_f.check\_box :has\_shipping\_address %\>  
\<%= addr\_f.text\_field :name %\>  
#more fields for the address…  
\<% end %\>  
\<% end %\>

The problem is that :reject\_if doesn’t seem to do it’s job. No matter  
what the value for has\_shipping\_address is, the save method is still  
called on the nested ShippingAddress, resulting in validation errors.  
Am I missing something here?  
Thanks.

---

<div class="post-metadata">

**Author:** ![andiug](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/6de8d8/32.png) [@andiug](https://www.ruby-forum.com/u/andiug)\
**Post date:** [June 25, 2009, 11:49am UTC](https://www.ruby-forum.com/t/accepts-nested-attributes-for-reject-if-issue/170964/2 "2009-06-25T11:49:10Z")

</div>

Andrei Don wrote:

> I have the following models:
> 
> class Order \< ActiveRecord::Base
> 
> belongs\_to :billing\_address  
> belongs\_to :shipping\_address
> 
> accepts\_nested\_attributes\_for :billing\_address  
> accepts\_nested\_attributes\_for :shipping\_address, :reject\_if =\> proc {  
> |attributes| attributes[‘has\_shipping\_address’] != ‘1’ }
> 
> def after\_initialize  
> self.build\_billing\_address unless billing\_address  
> self.build\_shipping\_address unless shipping\_address  
> end
> 
> end
> 
> class ShippingAddress \< OrderAddress  
> attr\_accessor :has\_shipping\_address  
> end
> 
> class OrderAddress \< ActiveRecord::Base  
> validates\_presence\_of :name  
> #more validations here…  
> end
> 
> And the view:
> 
> \<% form\_for @order do |f| %\>  
> #…  
> \<% f.fields\_for :shipping\_address do |addr\_f| %\>  
> \<%= addr\_f.check\_box :has\_shipping\_address %\>  
> \<%= addr\_f.text\_field :name %\>  
> #more fields for the address…  
> \<% end %\>  
> \<% end %\>
> 
> The problem is that :reject\_if doesn’t seem to do it’s job. No matter  
> what the value for has\_shipping\_address is, the save method is still  
> called on the nested ShippingAddress, resulting in validation errors.  
> Am I missing something here?  
> Thanks.

Turns out the :reject\_if didn’t work because I was doing the building of  
the nested shipping\_address in the after\_initialize callback of the  
order. After moving that to view (or a helper method), it works as  
expected.

def after\_initialize  
self.build\_billing\_address unless billing\_address  
end

#the view is now  
\<% form\_for @order do |f| %\>  
#…  
\<% @order.build\_shipping\_address unless @order.shipping\_address %\>  
\<% f.fields\_for :shipping\_address do |addr\_f| %\>  
\<%= addr\_f.check\_box :has\_shipping\_address %\>  
\<%= addr\_f.text\_field :name %\>  
#more fields for the address…  
\<% end %\>  
\<% end %\>
