ActiveRecord::Relation size vs count
An array in ruby has 3 methods that do the same thing. size
, count
, and length
all return the number of items in an array.
An ActiveRecord::Relation however uses them a bit differently. count
is always going to run a query in the database while size
will return the number of items in the collection based on the objects currently in the object graph.
> songs = Songs.all
> songs.size
10
> songs.count
SELECT count(*) FROM songs;
10
Tweet