find_all wierdness

O

Oleg Kh

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.
 
K

Kenosis

Oleg said:
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.
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
 
D

Daniel DeLorme

Oleg said:
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
 
O

Oleg Kh

Daniel said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,209
Messages
2,571,088
Members
47,686
Latest member
scamivo

Latest Threads

Top