When would a ruby array count not equal to the number of elements in an array?

779 views Asked by At

I'm running testunit (with machinist) and getting this very strange result when I run the ruby debugger

(rdb:1) @document.document_items
[]
(rdb:1) @document.document_items.count
2
(rdb:1) @document.document_items.length
0
(rdb:1) @document.document_items.size
0
(rdb:1) @document.document_items.class
Array
(rdb:1) @document
#<Document id: 1, title: "Recusandae quibusdam dicta deleniti et voluptate.", state: "published", site_id: nil, template_document_id: 2, most_important_message: "Quam totam corporis similique voluptatibus quaerat ...", delta: nil, approver_id: 1, author_id: 1, account_id: 1, updated_at: "2011-05-06 08:59:12", created_at: "2011-05-06 08:59:12">
(rdb:1) DocumentItem.find(:all)
[#<DocumentItem id: 1, title: "Et voluptatem officia voluptatem omnis voluptas.", body: "Nobis iste nostrum beatae corrupti ea qui debitis. ...", position: nil, document_id: 1, created_at: "2011-05-06 08:59:12", updated_at: "2011-05-06 08:59:12", version: 1, is_heading: false, help_message: nil, optional: nil, template_question_id: nil>, #<DocumentItem id: 2, title: "Ipsum in odio laborum ut officia.", body: "Quas temporibus iusto quidem non repellat. Quia des...", position: nil, document_id: 1, created_at: "2011-05-06 08:59:12", updated_at: "2011-05-06 08:59:12", version: 1, is_heading: false, help_message: nil, optional: nil, template_question_id: nil>]

A snippet of my Document/DocumentItem models:

class Document < ActiveRecord::Base
    ...
    has_many :document_items
    ...
end

class DocumentItem < ActiveRecord::Base
    ...
    belongs_to :document
    ...
end

Why is the document_items array count different to the number of elements in the document_items? Is it some kind of machinist magic? (could be related to: Ruby 1.92 in Rails 3: A Case where Array.length Does Not Equal Array.count?)

But the question that stems all this is, why is document_items empty? The connections are correctly set up, since this works:

(rdb:1) DocumentItem.first.document
#<Document id: 1, title: "Recusandae quibusdam dicta deleniti et voluptate.", state: "published", site_id: nil, template_document_id: 2, most_important_message: "Quam totam corporis similique voluptatibus quaerat ...", delta: nil, approver_id: 1, author_id: 1, account_id: 1, updated_at: "2011-05-06 08:59:12", created_at: "2011-05-06 08:59:12">
1

There are 1 answers

1
Sujoy Gupta On BEST ANSWER

This can happen as follows:

  1. @document object began with 0 document_items.
  2. Two DocumentItem objects were created directly, i.e., not through the @document.document_items association.

If you do not reload @document at this point, then length only returns the size of the document_items array cached in memory for the @document object, which is 0. However, count goes to the database, and returns 2 as expected.

In order to get around it, you need to explicitly call reload on @document after creating the new DocumentItem objects.