Skip to content

Instantly share code, notes, and snippets.

@gautamrege
Created October 31, 2013 02:51
Show Gist options
  • Save gautamrege/7243710 to your computer and use it in GitHub Desktop.
Save gautamrege/7243710 to your computer and use it in GitHub Desktop.
Saving Embedded document with :name option
class Address
include Mongoid::Document
field :street, type: String
field :city, type: String
field :state, type: String
field :zipcode, type: String
field :country, type: String
embedded_in :author, name: :permanent_address
embedded_in :author, name: :current_address
end
class Author
include Mongoid::Document
field :name, type: String
field :last_name, type: String
field :password, type: String
embeds_one :permanent_address, class_name: 'Address'
embeds_one :current_address, class_name: 'Address'
end
2.0.0p247 :001 > a = Author.all.sample
=> #<Author _id: 51fc058545db7c843f000fe2, name: "Elton Renner", last_name: nil, password: nil>
2.0.0p247 :002 > a.permanent_address = Address.new(city: "London")
=> #<Address _id: 5271c4f9476175a209000000, street: nil, city: "London", state: nil, zipcode: nil, country: nil, publisher_id: nil>
2.0.0p247 :003 > a.current_address = Address.new(city: "Madrid")
=> #<Address _id: 5271c506476175a209010000, street: nil, city: "Madrid", state: nil, zipcode: nil, country: nil, publisher_id: nil>
2.0.0p247 :004 >
Thu Oct 31 08:18:10.635 [conn14] getmore sodibee_development.authors cursorid:21582399651377187 ntoreturn:0 keyUpdates:0 locks(micros) r:2205 nreturned:9921 reslen:462589 2ms
Thu Oct 31 08:18:25.006 [conn14] update sodibee_development.authors query: { _id: ObjectId('51fc058545db7c843f000fe2') } update: { $set: { permanent_address: { _id: ObjectId('5271c4f9476175a209000000'), city: "London" } } } idhack:1 nmoved:1 nupdated:1 keyUpdates:0 locks(micros) w:259 0ms
Thu Oct 31 08:18:25.006 [conn14] run command sodibee_development.$cmd { getlasterror: 1, w: 1 }
Thu Oct 31 08:18:25.006 [conn14] command sodibee_development.$cmd command: { getlasterror: 1, w: 1 } ntoreturn:1 keyUpdates:0 reslen:85 0ms
Thu Oct 31 08:18:25.025 [journal] lsn set 5014223
Thu Oct 31 08:18:38.700 [conn14] update sodibee_development.authors query: { _id: ObjectId('51fc058545db7c843f000fe2') } update: { $set: { current_address: { _id: ObjectId('5271c506476175a209010000'), city: "Madrid" } } } idhack:1 nmoved:1 nupdated:1 keyUpdates:0 locks(micros) w:438 0ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment