4

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">
13
  • What is the output from @document.document_items? Commented May 6, 2011 at 9:25
  • It's in the first line, an empty array: [] Commented May 6, 2011 at 9:31
  • 1
    Ah, sorry. Do you have a default scope on your DocumentItem model? Commented May 6, 2011 at 9:33
  • What happens if you try @document.document_items.all.length ? Commented May 6, 2011 at 9:36
  • @document.document_items.all.length # gives 2 (just like count) Commented May 6, 2011 at 9:50

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! The problem was machinist didn't load the document object when document items were added.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.