Created
August 4, 2010 05:56
-
-
Save jacquescrocker/507721 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
puts ENV["GEM_HOME"] | |
require 'mongoid' | |
require 'mongoid/version' | |
puts "Using Mongoid: #{Mongoid::VERSION}" | |
Mongoid.master = Mongo::Connection.new.db("mongoid_playground") | |
class Animal | |
include Mongoid::Document | |
field :name, :type => String | |
end | |
class Dog < Animal | |
end | |
class Cat < Animal | |
end | |
Animal.delete_all | |
Dog.delete_all | |
Cat.delete_all | |
Animal.create :title => "animal1" | |
Animal.create :title => "animal2" | |
Dog.create :title => "fido" | |
Dog.create :title => "rex" | |
Cat.create :title => "mittens" | |
Cat.create :title => "arnold schwarzenegger" | |
# get all the animals | |
results = Animal.all | |
puts "\n\nAnimal.all gives #{results.count} records matching all Animal documents (including cats and dogs):" | |
results.entries.each{|r| puts r.inspect } | |
# get all the dogs | |
results = Dog.all | |
puts "\n\nDog.all gives #{results.count} records matching just Dog documents:" | |
results.entries.each{|r| puts r.inspect } | |
# get all the cats | |
results = Cat.all | |
puts "\n\nCat.all gives #{results.count} records matching just Cat documents:" | |
results.entries.each{|r| puts r.inspect } | |
# get all the animals (specifically Animal) | |
results = Animal.criteria.type("Animal") | |
puts "\n\nAnimal.criteria.type(\"Animal\") gives #{results.count} records of only exact match type Animal (not including dogs and cats):" | |
results.entries.each{|r| puts r.inspect } | |
# get all the dogs again (specifically from Animal) | |
results = Animal.criteria.type("Dog") | |
puts "\n\nAnimal.criteria.type(\"Dog\") gives #{results.count} records of only type Dog (not including other animals):" | |
results.entries.each{|r| puts r.inspect } | |
# get all the cats again (specifically from Animal) | |
results = Animal.criteria.type("Cat") | |
puts "\n\nAnimal.criteria.type(\"Cat\") gives #{results.count} records of only type Cat (not including other animals):" | |
results.entries.each{|r| puts r.inspect } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment