Sha256: 5e6beb2a9553c49e87ecc912582f6d2202ad44ae4d1cba79c45a2498e704822f

Contents?: true

Size: 964 Bytes

Versions: 2

Compression:

Stored size: 964 Bytes

Contents

require 'mongo_doc'

class Address
  include MongoDoc::Document

  key :street
  key :city
  key :state
  key :zip_code
  key :phone_number
end

class Contact
  include MongoDoc::Document

  key :name
  key :interests
  has_many :addresses

  scope :in_state, lambda {|state| where('addresses.state' => state)}
end

Contact.collection.drop

contact = Contact.new(:name => 'Hashrocket', :interests => ['ruby', 'rails', 'agile'])
contact.addresses << Address.new(:street => '320 1st Street North, #712', :city => 'Jacksonville Beach', :state => 'FL', :zip_code => '32250', :phone_number => '877 885 8846')
contact.save
puts Contact.find_one(contact.to_param).addresses.first.street

hashrocket = Contact.in_state('FL').find {|contact| contact.name == 'Hashrocket'}

hashrocket_address = hashrocket.addresses.first
hashrocket_address.update_attributes(:street => '320 First Street North, #712')

puts Contact.where(:name => 'Hashrocket').first.addresses.first.street

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
mongo_doc-0.3.1 examples/simple_document.rb
mongo_doc-0.3.0 examples/simple_document.rb