views:

42

answers:

1

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

A: 

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.

Emily