ActiveRecord: size vs count
You should read that, it's still valid.
You'll adapt the function you use depending on your needs.
if you already load all entries, say
User.all, then you should use
lengthto avoid another db query
if you haven't anything loaded, use
countto make a count query on your db
if you don't want to bother with these considerations, use
sizewhich 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:
as described here:
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
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?
size all do pretty much the same thing regarding arrays. See here for more info.
When using ActiveRecord objects, however,
count is better than
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
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).
Just in case the image gets taken down, basically:
lengthalways pulls all the records and then calls .length on the array - bad
countalways does a count query - good
sizelooks 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
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 :