ruby 数组计数何时不等于数组中元素的数量?
我正在运行 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能会发生如下:
@document
对象以 0document_items
开头。DocumentItem
对象是直接创建的,即不是通过@document.document_items
关联创建的。如果此时不重新加载
@document
,则length
仅返回@document
对象在内存中缓存的 document_items 数组的大小,即 0。但是,count
会转到数据库,并按预期返回 2。为了解决这个问题,您需要在创建新的
DocumentItem
对象后显式调用@document
上的reload
。This can happen as follows:
@document
object began with 0document_items
.DocumentItem
objects were created directly, i.e., not through the@document.document_items
association.If you do not reload
@document
at this point, thenlength
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 newDocumentItem
objects.