find(:all) and find_all_<etc>

Raja Venkataraman wrote:

Thats the way its defined in ActiveRecord. It returns an array of models for the sql results and returns it. So when there are no matches, it returns an empty array.

I guess my question is: why does ActiveRecord define it this way? It would seem to be more consistent with the way everything else works in ruby for a function that has no results to return nil, instead of returning an empty array.

I would disagree... For me, if I'm doing a find(:all...) I'm going to expect that 99% of the time I'm going to get back some data... and odds are I'm going to loop through them.

So, by returning an empty array I can do that without having to check if the result is nil first and getting that nasty "you tried to call nil.each" error.

-philip