Find(:all) and find_all_<etc>

Perhaps I’m an idiot and just don’t get it, and perhaps this question
has been asked a million times before, but I’m not really sure how to go
about searching for an answer to it:

Why does my code Library.find(:all, :conditions => “user_id =
#{session[:user].id}”) return [] instead of nil when there are no
results?

Luke wrote:

Perhaps I’m an idiot and just don’t get it, and perhaps this question
has been asked a million times before, but I’m not really sure how to go
about searching for an answer to it:

Why does my code Library.find(:all, :conditions => “user_id =
#{session[:user].id}”) return [] instead of nil when there are no
results?

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.

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.

for consistency with its normal results, i’d think. it usually returns
an array, and so code gets written expecting to handle an array,
therefore it returns an empty array instead of nil.

Luke wrote:

Perhaps I’m an idiot and just don’t get it, and perhaps this question
has been asked a million times before, but I’m not really sure how to go
about searching for an answer to it:

Why does my code Library.find(:all, :conditions => “user_id =
#{session[:user].id}”) return [] instead of nil when there are no
results?


Lance I.
Web Applications Developer
RBS Interactive
[email protected]

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

Perhaps I’m an idiot and just don’t get it, and perhaps this question
has been asked a million times before, but I’m not really sure how to go
about searching for an answer to it:

Why does my code Library.find(:all, :conditions => “user_id =
#{session[:user].id}”) return [] instead of nil when there are no
results?

Because the docs say so :slight_smile:

Find first: This will return the first record matched by the options
used.
These options can either be specific conditions or merely an order. If
no
record can matched, nil is returned.

Find all: This will return all the records matched by the options used.
If
no records are found, an empty array is returned.