MongoMapper - 使用新键更新现有记录
当通过 MongoMapper 将密钥添加到现有模型(包含现有数据)时,我可以使用新密钥创建新文档,但是当尝试使用同一密钥访问现有文档时,它会失败,并指出它是“未定义的方法”。
我想知道是否有人有任何见解。
提前致谢!
(是的,这些示例被截断了。)
- model.rb -
key :key_1
key :key_2
- would return -
#<Model _id: BSON::ObjectID('4ba821abebddb9094c000001'), key_1: "test", key_2: "test">
- model.rb (updated version) -
key :key_1
key :key_2
key :key_3
- would still only return -
#<Model _id: BSON::ObjectID('4ba821abebddb9094c000001'), key_1: "test", key_2: "test">
- but if a new doc is created -
#<Model _id: BSON::ObjectID('4ba821abebddb9094c000001'), key_1: "test", key_2: "test">
#<Model _id: BSON::ObjectID('7ba131abedaab9094c007482'), key_1: "test", key_2: "test", key_3: "test">
这个没问题,除了我在尝试访问第一个文档的 :key_3 时收到方法未定义错误这一事实。
Rails 2.3.4
MongoMapper 0.7.4
When adding a key to an existing model (with existing data) via MongoMapper, I can create new documents with the new key but when trying to access existing documents using that same key it fails stating that it's an "undefined method."
I was wondering if anyone had any insight.
Thanks in advance!
(Yes, these examples are truncated.)
- model.rb -
key :key_1
key :key_2
- would return -
#<Model _id: BSON::ObjectID('4ba821abebddb9094c000001'), key_1: "test", key_2: "test">
- model.rb (updated version) -
key :key_1
key :key_2
key :key_3
- would still only return -
#<Model _id: BSON::ObjectID('4ba821abebddb9094c000001'), key_1: "test", key_2: "test">
- but if a new doc is created -
#<Model _id: BSON::ObjectID('4ba821abebddb9094c000001'), key_1: "test", key_2: "test">
#<Model _id: BSON::ObjectID('7ba131abedaab9094c007482'), key_1: "test", key_2: "test", key_3: "test">
This would be fine except for the fact that I receive a method undefined error when trying to access :key_3 for the first document.
Rails 2.3.4
MongoMapper 0.7.4
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使在与更新类之前实例化的对象进行交互时,我也根本没有看到这种行为。在 irb 中运行以下命令时,我没有错误:
由于您似乎遇到了不寻常的问题,因此您的用例的更多详细信息将会有所帮助。您能给我们一个更完整的代码示例吗?如果代码中存在失败的专有内容,请将其削减到仍然失败所需的最小情况,并发布模型的完整声明以及用于访问它们的代码。
I'm not seeing this behavior at all, even when interacting with an object instantiated before I updated the class. When running the following in irb, I have no errors:
Since it seems like you're having an unusual problem, more details of your use-case would be helpful. Could you please give us a more complete code example? If you've got proprietary stuff in the code that's failing, pare it down to the minimum case required to still have it fail and post the complete declarations of your models and the code you're using to access them.
使用 set 命令...
此代码将为您的模型创建一个新字段,确认您已经使用新键进行了定义:
享受,
Use the set command...
This code will create a new field for your model, confirm that you already did defined with the new key:
Enjoy,