Sha256: 2fbbdb44200a333d96e05f61c9d6e8f4bf3c06996e3b6c5dae1df13a3901dc9f

Contents?: true

Size: 1.29 KB

Versions: 32

Compression:

Stored size: 1.29 KB

Contents

require 'mongo_doc'

class Address
  include MongoDoc::Document

  attr_accessor :street
  attr_accessor :city
  attr_accessor :state
  attr_accessor :zip_code
  attr_accessor :phone_number
end

class Contact
  include MongoDoc::Document

  attr_accessor :name
  attr_accessor :interests
  embed_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

# Finders
Contact.find_all.each {|c| puts c.name}
puts contact.to_param
puts Contact.find_one(contact.to_param).addresses.first.street
Contact.find(contact.to_param).each {|c| puts c.name}

hashrocket_in_fl = Contact.in_state('FL').where(:name => /rocket/)

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

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

# Criteria behave like new AR3 AREL queries
hr = Contact.where(:name => 'Hashrocket')
hr_in = hr.where('addresses.state' => 'IN')
puts hr.count
puts hr_in.count

Version data entries

32 entries across 32 versions & 2 rubygems

Version Path
mongo_doc-0.6.26 examples/simple_document.rb
mongo_doc-0.6.25 examples/simple_document.rb
mongo_doc-0.6.23 examples/simple_document.rb
mongo_doc-0.6.22 examples/simple_document.rb
mongo_doc-0.6.21 examples/simple_document.rb
mongo_doc-0.6.20 examples/simple_document.rb
mongo_doc-0.6.19 examples/simple_document.rb
mongo_doc-0.6.18 examples/simple_document.rb
mongo_doc-0.6.17 examples/simple_document.rb
mongo_doc-0.6.16 examples/simple_document.rb
mongo_doc-0.6.15 examples/simple_document.rb
mongo_doc-0.6.14 examples/simple_document.rb
mongo_doc-0.6.13 examples/simple_document.rb
mongo_doc-0.6.12 examples/simple_document.rb
mongo_doc-0.6.11 examples/simple_document.rb
mongo_doc-0.6.10 examples/simple_document.rb
mongo_doc-0.6.9 examples/simple_document.rb
mongo_doc-0.6.8 examples/simple_document.rb
mongo_doc-0.6.7 examples/simple_document.rb
mongo_doc-0.6.6 examples/simple_document.rb