In Rails, you can find the number of records using both Model.size
and Model.count
. If you're dealing with more complex queries is there any advantage to using one method over the other? How are they different?
For instance, I have users with photos. If I want to show a table of users and how many photos they have, will running many instances of user.photos.size
be faster or slower than user.photos.count
?
Thanks!
Best Answer
You should read that, it's still valid.
You'll adapt the function you use depending on your needs.
Basically:
if you already load all entries, say
User.all
, then you should uselength
to avoid another db queryif you haven't anything loaded, use
count
to make a count query on your dbif you don't want to bother with these considerations, use
size
which will adapt