Activerecord: Size VS Count

ActiveRecord: size vs count

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 use length to avoid another db query

  • if you haven't anything loaded, use count to make a count query on your db

  • if you don't want to bother with these considerations, use size which will adapt

Understanding rails size vs count, and why .size doesn't work in view?

count is an AR method, it will use COUNT in the SQL query, and it actually makes AR aware that you are doing a database query, and it return the count value returned by the database.

Size is a method for collections, Post and User are classes, not collections, so AR is never used, and you end up with an error, because you first need to do an AR query to have a collection, even an empty one.

The rails way to do what you try is to use all, that will return a collection.

@post_count = Post.all.size
@user_count = User.all.size

Ruby on Rails ActiveRecord size on association returns twice the amount of count after duplicating a model

So, after Aakash Gupta and Szymon said I'm doing something wrong somewhere else I found out that my association class has this line:

belongs_to :model, counter_cache: :association_count

Which is there to make the size operator more efficient and skip database querys. However after duplicating objects my cache was corrupted. So calling:

Model.reset_counters(new_model.id, :association)

as described here:
https://apidock.com/rails/ActiveRecord/CounterCache/reset_counters
fixed my problem.

size, length and count in Rails

length will load all your objects just to count them; something like:

select * from addresses...

and then return the results count.
As you can imagine - it's bad performance

count will just issue

select count(*) from addresses...

which is better, because we are not loading all addresses just to count them

size is smarter - it'll check if the association is already loaded and if true then return the length (without issuing a call to the database).

size also checks for counter_cache if you have a field named address_count in your user model, then size will use this field for the count, so there is no need to issue a count on the addresses table.

if all fails, size will issue a select count(*) on the database

Confusing difference between `count` and `size`

Check this documentation out on size for Rails: Rails ActiveRecord Size Documentation

Also the documentation for count is here as well: Rails ActiveRecord Count Documentation


There are Ruby AND ActiveRecord methods length, size, and count which are completely different from each other.




Your first example of:>> @order.products.count is attempting to call the Rails ActiveRecord count method (counting records in the DB) while your other example of >> @order.products.to_a.count is attempting to call the Ruby count method (counting items in the container within memory with no connection to the DB).




So to answer your question when using the >> @order = Order.new(products: [my_product]) you are only creating the object in memory and not within the DB. You can read the documentation on size I posted a link to above to tell you why it is able to tell you either the length of the collection or the count of records in the DB depending on the context of its use.

Hope this helps!

Which is faster count or length?

In ruby, count, length and size all do pretty much the same thing regarding arrays. See here for more info.

When using ActiveRecord objects, however, count is better than length, and size is even better.

find_all_by_country returns a dumb array so you shouldn't use that method (because it always returns an array). Instead, use where(country: params[:country]).

I'll let Code School's Rails Best Practices slide nº 93 speak for itself (and hope they don't get mad at me for reproducing it here).

enter image description here

Just in case the image gets taken down, basically:

  1. length always pulls all the records and then calls .length on the array - bad

  2. count always does a count query - good

  3. size looks at the cache if you have a cache counter, otherwise does a count query - best

Rails count vs. size model records when creating new record

You can use count method instead of use size.
For explanation, you need to know how size performs :

  • If a collection is loaded, the method will count elements in collection ( as an array )
  • If there is no loaded collection, the method perform a query.

In your situation, you want to perform a query as count method do :

current_user.cart_items.count



Related Topics



Leave a reply



Submit