Daniel DeLorme
7/30/2006 3:57:00 PM
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