Ruby on Rails Developer Question:
Download Questions PDF

Explain what’s different between ActiveRecord::Relation’s count, length and size methods?

Answer:

☛ 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.

Download Ruby on Rails Developer Interview Questions And Answers PDF

Previous QuestionNext Question
Please explain request/response cycle?Tell me how does Rails implement AJAX?