ruby 数组计数何时不等于数组中元素的数量?

发布于 2024-11-05 07:20:57 字数 2341 浏览 5 评论 0原文

我正在运行 testunit(与机械师),并在运行 ruby​​ 调试器时得到这个非常奇怪的结果

(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>]

我的 Document/DocumentItem 模型的片段:

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

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

为什么 document_items 数组计数与 document_items 中的元素数量不同?这是某种机械师的魔法吗? (可能与:Rails 3 中的 Ruby 1.92:Array.length 不等于 Array.count 的情况?

但是引发这一切的问题是,为什么 document_items 为空?连接已正确设置,因为这有效:

(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">

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">

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

看海 2024-11-12 07:20:57

这可能会发生如下:

  1. @document 对象以 0 document_items 开头。
  2. 两个 DocumentItem 对象是直接创建的,即不是通过 @document.document_items 关联创建的。

如果此时不重新加载 @document,则 length 仅返回 @document 对象在内存中缓存的 document_items 数组的大小,即 0。但是,count 会转到数据库,并按预期返回 2。

为了解决这个问题,您需要在创建新的 DocumentItem 对象后显式调用 @document 上的 reload

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文