[ruby-on-rails] ActiveRecord: size vs count
As the other answers state:
countwill perform an SQL
lengthwill calculate the length of the resulting array
sizewill try to pick the most appropriate of the two to avoid excessive queries
But there is one more thing. We noticed a case where
size acts differently to
lengthaltogether, and I thought I'd share it since it is rare enough to be overlooked.
If you use a
sizewill use the cached count directly, and not make an extra query at all.
class Image < ActiveRecord::Base belongs_to :product, counter_cache: true end class Product < ActiveRecord::Base has_many :images end > product = Product.first # query, load product into memory > product.images.size # no query, reads the :images_count column > product.images.count # query, SQL COUNT > product.images.length # query, loads images into memory
This behaviour is documented in the Rails Guides, but I either missed it the first time or forgot about it.
In Rails, you can find the number of records using both
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
The following strategies all make a call to the database to perform a
Model.count Model.all.size records = Model.all records.count
The following is not as efficient as it will load all records from the database into Ruby, which then counts the size of the collection.
records = Model.all records.size
If your models have associations and you want to find the number of belonging objects (e.g.
@customer.orders.size), you can avoid database queries (disk reads). Use a counter cache and Rails will keep the cache value up to date, and return that value in response to the