Could I declare a model with a key called :key
, for instance? Is there any word I can't use for a key?
views:
22answers:
2
+1
A:
The first question if very easy to answer yourself. Open irb and try:
>> require 'mongo_mapper'
=> true
>> MongoMapper.database = 'test'
=> "test"
>> class Test
>> include MongoMapper::Document
>> key :key
>> end
=> #<MongoMapper::Plugins::Keys::Key:0x101fc7a90 @default_value=nil, @type=nil, @name="key", @options={}>
>> t = Test.new(:key => 'value')
=> #<Test _id: BSON::ObjectID('4c4dcced7123374587000001'), key: "value">
>> t.save
=> true
>> Test.all
=> [#<Test _id: BSON::ObjectID('4c4dcced7123374587000001'), key: "value">]
No errors? I guess key
is a valid key!
As far as I know, the only keys you shouldn't use for your own data are _id
and _type
. You can use either, but they will change behavior. Using _id
will make whatever you're setting as that key the unique id for the object. Using _type
will cause MongoMapper to try to instantiate an instance of whatever's in your _test
key when bringing the object back from the database.
Emily
2010-07-26 18:03:07
+1
A:
_id and _type. Also, any thing that would create a method the same as a mongomapper doc/edoc instance method, such as associations, etc.
John Nunemaker
2010-08-04 04:48:45