MongoMapper - 使用新键更新现有记录

发布于 2024-09-07 09:09:17 字数 862 浏览 0 评论 0原文

当通过 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 技术交流群。

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

发布评论

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

评论(2

葬花如无物 2024-09-14 09:09:17

即使在与更新类之前实例化的对象进行交互时,我也根本没有看到这种行为。在 irb 中运行以下命令时,我没有错误:

>> gem 'mongo_mapper', '0.7.4'
=> true
>> require 'mongo_mapper'
=> true
>> MongoMapper.database = 'test'
=> "test"
>> class Foo
>>   include MongoMapper::Document
>>   key :something
>>   end
=> #<MongoMapper::Plugins::Keys::Key:0x101f8f938 @default_value=nil, @type=nil, @name="something", @options={}>
>> f = Foo.new(:something => 'thing')
=> #<Foo _id: ObjectID('4c4dc9af712337447c000001'), something: "thing">
>> f.save
=> true
>> f
=> #<Foo _id: ObjectID('4c4dc9af712337447c000001'), something: "thing">
>> class Foo
>>   key :something_else
>>   end
=> #<MongoMapper::Plugins::Keys::Key:0x101f6ad90 @default_value=nil, @type=nil, @name="something_else", @options={}>
>> f
=> #<Foo something_else: nil, _id: ObjectID('4c4dc9af712337447c000001'), something: "thing">
>> f.something_else
=> nil

由于您似乎遇到了不寻常的问题,因此您的用例的更多详细信息将会有所帮助。您能给我们一个更完整的代码示例吗?如果代码中存在失败的专有内容,请将其削减到仍然失败所需的最小情况,并发布模型的完整声明以及用于访问它们的代码。

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:

>> gem 'mongo_mapper', '0.7.4'
=> true
>> require 'mongo_mapper'
=> true
>> MongoMapper.database = 'test'
=> "test"
>> class Foo
>>   include MongoMapper::Document
>>   key :something
>>   end
=> #<MongoMapper::Plugins::Keys::Key:0x101f8f938 @default_value=nil, @type=nil, @name="something", @options={}>
>> f = Foo.new(:something => 'thing')
=> #<Foo _id: ObjectID('4c4dc9af712337447c000001'), something: "thing">
>> f.save
=> true
>> f
=> #<Foo _id: ObjectID('4c4dc9af712337447c000001'), something: "thing">
>> class Foo
>>   key :something_else
>>   end
=> #<MongoMapper::Plugins::Keys::Key:0x101f6ad90 @default_value=nil, @type=nil, @name="something_else", @options={}>
>> f
=> #<Foo something_else: nil, _id: ObjectID('4c4dc9af712337447c000001'), something: "thing">
>> f.something_else
=> nil

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.

暖树树初阳… 2024-09-14 09:09:17

使用 set 命令...

    @model.set(:key_3 => "VALUE...")
    @model.reload
    @model.key_3 # => "VALUE..."
    @model.save

此代码将为您的模型创建一个新字段,确认您已经使用新键进行了定义:

    key :key_3

享受,

Use the set command...

    @model.set(:key_3 => "VALUE...")
    @model.reload
    @model.key_3 # => "VALUE..."
    @model.save

This code will create a new field for your model, confirm that you already did defined with the new key:

    key :key_3

Enjoy,

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