# Find\_all wierdness

**URL:** <https://www.ruby-forum.com/t/find-all-wierdness/100422>\
**Category:** Ruby\
**Created:** [July 28, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/find-all-wierdness/100422 "2006-07-28T21:46:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gbh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gbh](https://www.ruby-forum.com/u/gbh)\
**Post date:** [July 28, 2006, 9:46pm UTC](https://www.ruby-forum.com/t/find-all-wierdness/100422/1 "2006-07-28T21:46:31Z")

</div>

My problem is as follows:

book.chapters.find\_all{ |chapter| !chapter.pages.empty? }.size

Theoretically it should return a number of chapters that have pages in  
them. However it doesn’t do that at all. It simply returns a number of  
all chapters.

On the same note:

book.chapters.find\_all { |chapter| chapter.pages.empty? }.size returns  
the same thing as above.

Identically

book.chapters.find\_all { |chapter| 1 == 2 }.size still returns same  
thing (shouldn’t that be zero now???)

And finally

book.chapters.find\_all{ my ass }.size returns exactly the same thing!!!  
Seems that whatever is in brackets is simply ignored.

Any ideas why it does that? BTW, it seems to work just fine for  
numerical arrays.

Thanks.

---

<div class="post-metadata">

**Author:** ![gbh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gbh](https://www.ruby-forum.com/u/gbh)\
**Post date:** [July 28, 2006, 10:36pm UTC](https://www.ruby-forum.com/t/find-all-wierdness/100422/2 "2006-07-28T22:36:53Z")

</div>

Oleg Kh wrote:

> book.chapters.find\_all { |chapter| chapter.pages.empty? }.size returns  
> Seems that whatever is in brackets is simply ignored.
> 
> Any ideas why it does that? BTW, it seems to work just fine for  
> numerical arrays.
> 
> Thanks.
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> Can you send any more context for your use? I’ve used find\_all on many  
> types of arrays w/ .size appended to a block and have no problems with  
> it. Just ran a couple of IRB tests too and all’s well. Could it be  
> that your book.chapters is really empty? Have you tried adding a print  
> to your block to ensure its ever being called. Notice that you change  
> the block contents and always get the same result: this implies to me  
> the block is not the issue but what the enumerable has in it, if  
> anything is.

Cheers,

Ken

---

<div class="post-metadata">

**Author:** ![gbh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gbh](https://www.ruby-forum.com/u/gbh)\
**Post date:** [August 1, 2006, 10:41pm UTC](https://www.ruby-forum.com/t/find-all-wierdness/100422/3 "2006-08-01T22:41:59Z")

</div>

Daniel DeLorme wrote:

> Try to use select instead of find\_all. In this case I think you are  
> actually  
> triggering ActiveRecord’s find\_all instead of the Enumerable#find\_all  
> that you  
> were expecting. book.chapters is not an array but an association object.  
> The  
> ActiveRecord version is defined as:  
> Project#milestones.find\_all(conditions)  
> So in your case, you don’t give any conditions as parameters and the  
> method just  
> ignores the block and returns all chapters. Sometimes Rails magic can  
> bite you  
> in the ass.
> 
> Daniel

Thank you Daniel. That was it.

---

<div class="post-metadata">

**Author:** ![gbh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/9fc348/32.png) [@gbh](https://www.ruby-forum.com/u/gbh)\
**Post date:** [July 30, 2006, 5:58pm UTC](https://www.ruby-forum.com/t/find-all-wierdness/100422/4 "2006-07-30T17:58:19Z")

</div>

Oleg Kh wrote:

> My problem is as follows:
> 
> book.chapters.find\_all{ |chapter| !chapter.pages.empty? }.size
> 
> Theoretically it should return a number of chapters that have pages in  
> them. However it doesn’t do that at all. It simply returns a number of  
> all chapters.

Try to use select instead of find\_all. In this case I think you are  
actually  
triggering ActiveRecord’s find\_all instead of the Enumerable#find\_all  
that you  
were expecting. book.chapters is not an array but an association object.  
The  
ActiveRecord version is defined as:  
Project#milestones.find\_all(conditions)  
So in your case, you don’t give any conditions as parameters and the  
method just  
ignores the block and returns all chapters. Sometimes Rails magic can  
bite you  
in the ass.

Daniel
