MongoMapper新记录包含旧记录的数据
在我们的 Rails 应用程序环境(ruby 1.8.7 / Rails 2.3.12)中,我可以运行以下代码:
class MongoTester
include MongoMapper::Document
key :test_arr, Array, :default => []
end
mt = MongoTester.new
mt.test_arr << 24
mt2 = MongoTester.new
mt2.inspect 的输出是:
#<MongoTester test_arr: [24], _id: BSON::ObjectId('4e5c089f3beaacad00000002')>
我不确定这是怎么可能的。两条记录的 object_id 和 _id 不同。两人都没有得救。我们没有在我们的应用程序中修改 MongoMapper (mongomapper 0.8.6)。然而,一个全新的记录包含先前创建的不同记录的数据。
我无法在 MongoMapper 的测试套件中重现这一点。
任何关于这种效果如何可能或我如何摆脱它的信息都会很棒。谢谢!
In our Rails app environment (ruby 1.8.7 / rails 2.3.12) I can run the following code:
class MongoTester
include MongoMapper::Document
key :test_arr, Array, :default => []
end
mt = MongoTester.new
mt.test_arr << 24
mt2 = MongoTester.new
The output of mt2.inspect is:
#<MongoTester test_arr: [24], _id: BSON::ObjectId('4e5c089f3beaacad00000002')>
I'm not sure how this is possible. object_id and _id for both records are different. Neither one is saved. We haven't modified MongoMapper in our application (mongomapper 0.8.6). Yet a completely new record contains the data of a previously created different record.
I can't reproduce this in MongoMapper's test suite.
Any information on how this effect is possible or how I can get rid of it would be awesome. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个错误:将其提交到 https://github.com/jnunemaker/mongomapper/ issues/new此错误已在较新版本的 MongoMapper 中修复这是一个解决方法:
key :test_arr, Array, :default => lambda { [] }
这是因为 Ruby 中的数组是可变的,因此每次使用
<<
都会添加默认数组对象。您的文档的 object_id 将不同,但数组的 object_id 将相同。That is a bug: file it at https://github.com/jnunemaker/mongomapper/issues/newThis bug is fixed in newer versions of MongoMapperHere's a workaround:
key :test_arr, Array, :default => lambda { [] }
It's because arrays in Ruby are mutable, so your default array object is getting added to each time with
<<
. The object_id's of your docs will be different, but the object_id's of the arrays will be the same.