# Rails way to strip empty child objects from array?

**URL:** <https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453>\
**Category:** Rails\
**Created:** [February 4, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453 "2007-02-04T20:48:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![poipu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@poipu](https://www.ruby-forum.com/u/poipu)\
**Post date:** [February 4, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453/1 "2007-02-04T20:48:56Z")

</div>

Hi,  
I have a object with children. some of these children are empty…so  
when i paginate them, i get blanks. is there a way to strip out the  
empty children? these are my debug. thanks.

- !ruby/object:Scategory  
attributes:  
name: bbq  
id: “2”  
mcategory\_id: “2”  
howtos: []

- !ruby/object:Scategory  
attributes:  
name: baking  
id: “3”  
mcategory\_id: “2”  
howtos:

---

<div class="post-metadata">

**Author:** ![poipu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@poipu](https://www.ruby-forum.com/u/poipu)\
**Post date:** [February 4, 2007, 9:01pm UTC](https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453/2 "2007-02-04T21:01:56Z")

</div>

it seems basically i need to run a compact on the children, but how to  
use compact on children?

---

<div class="post-metadata">

**Author:** ![poipu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@poipu](https://www.ruby-forum.com/u/poipu)\
**Post date:** [February 4, 2007, 9:04pm UTC](https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453/3 "2007-02-04T21:04:15Z")

</div>

poipu wrote:

> I have a object with children. some of these children are empty…so  
> when i paginate them, i get blanks. is there a way to strip out the  
> empty children? these are my debug. thanks.

I have been having so much fun with Array#reject recently…

😉

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![poipu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@poipu](https://www.ruby-forum.com/u/poipu)\
**Post date:** [February 4, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453/4 "2007-02-04T21:29:18Z")

</div>

poipu wrote:

> it seems basically i need to run a compact on the children, but how to  
> use compact on children?

Put them into an Array?

If they are in a Model, such as a product of has\_many, they likely are  
already in an Array.

Post some Ruby code (not YAML!).

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![poipu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@poipu](https://www.ruby-forum.com/u/poipu)\
**Post date:** [February 4, 2007, 11:57pm UTC](https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453/5 "2007-02-04T23:57:30Z")

</div>

poipu wrote:

> @scats = Scategory.find(:all, :conditions =\> [“mcategory\_id = ?”,  
> params[:id]], :include =\> :howtos)
> 
> howtos are the children…scategory has many howtos…
> 
> now scats is an array where the content is an array of howtos contents?

p scats.first  
p scats.first.howtos  
p scats.first.howtos.first

## Next question: What is an “empty” howto? Can you add a :condition or similar to the has\_many to exclude them at the database level? Or - even better

not store them?

–  
Phlip  
[Redirecting...](http://www.greencheese.us/ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![poipu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/9dc877/32.png) [@poipu](https://www.ruby-forum.com/u/poipu)\
**Post date:** [February 4, 2007, 9:46pm UTC](https://www.ruby-forum.com/t/rails-way-to-strip-empty-child-objects-from-array/84453/6 "2007-02-04T21:46:41Z")

</div>

yes, it is a model. but im still trying to grasp the concept…

i am calling this…

@scats = Scategory.find(:all, :conditions =\> [“mcategory\_id = ?”,  
params[:id]], :include =\> :howtos)

howtos are the children…scategory has many howtos…

now scats is an array where the content is an array of howtos contents?
