Skip to content

Instantly share code, notes, and snippets.

@fredrikhenne
Created May 1, 2010 21:04
Show Gist options
  • Save fredrikhenne/386654 to your computer and use it in GitHub Desktop.
Save fredrikhenne/386654 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("mongoid_pet")
end
Mongoid.master.collection("villages").drop
Mongoid.master.collection("villagers").drop
Mongoid.master.collection("pets").drop
class Village
include Mongoid::Document
field :name, :type => String
has_many_related :villagers
end
class Villager
include Mongoid::Document
field :village_id, :type => String
field :name, :type => String
has_many_related :pets
belongs_to_related :village
end
class Pet
include Mongoid::Document
field :villager_id, :type => String
field :name, :type => String
field :species, :type => String
belongs_to_related :villager
end
2.times do
@village = Village.create!(:name => "Charlesville")
2.times do
@villagers = Villager.create!(:name => "James Brown", :village_id => @village.id)
2.times do
@pet = Pet.create!(:name => "Timmy", :species => "bird", :villager_id => @villagers.id)
@pet = Pet.create!(:name => "Timmy", :species => "cat", :villager_id => @villagers.id)
end
end
end
@data = Pet.only(:species).aggregate
puts @data.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment