# Rejecting a nested object if all attributes are blank

**URL:** <https://www.ruby-forum.com/t/rejecting-a-nested-object-if-all-attributes-are-blank/186936>\
**Category:** Rails\
**Created:** [April 14, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/rejecting-a-nested-object-if-all-attributes-are-blank/186936 "2010-04-14T16:27:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 14, 2010, 4:27pm UTC](https://www.ruby-forum.com/t/rejecting-a-nested-object-if-all-attributes-are-blank/186936/1 "2010-04-14T16:27:11Z")

</div>

How do I reject the creation of an object if all of the attributes are  
blank?

accepts\_nested\_attributes\_for :questions, :allow\_destroy =\>  
true, :reject\_if =\> lambda {|a| a.each.blank? }

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 14, 2010, 5:40pm UTC](https://www.ruby-forum.com/t/rejecting-a-nested-object-if-all-attributes-are-blank/186936/2 "2010-04-14T17:40:23Z")

</div>

You can do like this using all? method

accepts\_nested\_attributes\_for :questions, :allow\_destroy =\> true,  
:reject\_if =\> lambda { |attrs| attrs.all? { |k, v| v.blank?}}

–  
Thanks,  
Abhinav  
[http://twitter.com/abhinav](http://twitter.com/abhinav)

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 21, 2010, 10:43am UTC](https://www.ruby-forum.com/t/rejecting-a-nested-object-if-all-attributes-are-blank/186936/3 "2010-04-21T10:43:18Z")

</div>

that works, thanks!

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 28, 2010, 12:19pm UTC](https://www.ruby-forum.com/t/rejecting-a-nested-object-if-all-attributes-are-blank/186936/4 "2010-04-28T12:19:47Z")

</div>

Does that still reject the object if the id attribute is not blank?  
Do I have to test each attribute separately and exclude the id  
attribute in order for that to work?
