Difference between count, length and size in an association with ActiveRecord

21-Apr-2022 1354
With Rails/ActiveRecord, you can count the records in an association using three methods .count, .length, .size.As a rails newbie, when you first discover this, it can be confusing… Why do we need three methods? Is there any advantage to using one method over the other? How are they different?Here are 6 characteristics of this three methods that you should know. They will help you decide which method to use on different situations.I will also share a why I think that most of the time you should start with .size and the reference to the docs where you can find more information.
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle