Skip to content

Instantly share code, notes, and snippets.

@rhyhann
Created December 24, 2009 10:44
Show Gist options
  • Save rhyhann/263140 to your computer and use it in GitHub Desktop.
Save rhyhann/263140 to your computer and use it in GitHub Desktop.
>> require 'mongo_mapper'
=> true
>> MongoMapper.database = 'itsatest'
=> "itsatest"
>> class Person
>> include MongoMapper::Document
>> key :first_key
>> end
=> #<MongoMapper::Key:0x101fe92a8 @name="first_key", @type=nil, @default_value=nil, @options={}>
>> first = Person.new(:first_key => 'first_value')
=> #<Person _id: nil, first_key: "first_value">
>> first.save
=> true
>> class Person
>> key :second_key
>> end
=> #<MongoMapper::Key:0x101fd50c8 @name="second_key", @type=nil, @default_value=nil, @options={}>
>> Person.new
=> #<Person second_key: nil, _id: nil, first_key: nil>
>> first.second_key = 'second_value'
MongoMapper::KeyNotFound: Could not find key: "second_key"
from /Library/Ruby/Gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/embedded_document.rb:361:in `read_attribute'
from /Library/Ruby/Gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/dirty.rb:78:in `send'
from /Library/Ruby/Gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/dirty.rb:78:in `clone_key_value'
from /Library/Ruby/Gems/1.8/gems/mongo_mapper-0.6.8/lib/mongo_mapper/dirty.rb:118:in `write_attribute'
from (eval):10:in `second_key='
from (irb):13
>> first
=> #<Person _id: 4b3345d833e0ad021d000001, first_key: "first_value">
>> exit
$ irb
>> require 'mongoid'
=> true
>> connection = Mongo::Connection.new('localhost')
=> #<Mongo::Connection:...>
>> Mongoid.database = connection.db('anotherproof')
=> #<Mongo::DB:...>>
>> class Person < Mongoid::Document
>> field :first_key
>> end
=> nil
>> first = Person.new(:first_key => 'first_value')
=> Person : {"_id"=>"4b33471633e0ad0227000001", "first_key"=>"first_value"}
>> first.save
=> Person : {"_id"=>"4b33471633e0ad0227000001", "first_key"=>"first_value"}
>> class Person < Mongoid::Document
>> field :second_key
>> end
=> nil
>> first.second_key = 'second_value'
=> "second_value"
>> first.save
=> true
>> first
=> Person : {"second_key"=>"second_value", "_id"=>"4b33471633e0ad0227000001", "first_key"=>"first_value"}
>> exit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment