Answers

Question and Answer:

  Home  Ruby on Rails Developer

⟩ Explain what’s different between ActiveRecordRelation’s count, length and size methods?

☛ count – counts the number of elements using query with the SQL command ‘COUNT’ but the result is not stored internally during object life cycle. This means, each time we invoke this method, SQL query is performed again. You should use this method if you don’t have anything loaded.

☛ length – loads all objects just to count them and then return the result count. It should be used only if you have already loaded all entries to avoid another database query.

☛ size – returns the size of the collection. If a collection is loaded, it will count its elements without any database query; but if a collection is not loaded, it will perform an additional query.

So we can say that size adapts to the situation.

 216 views

More Questions for you: