Skip to content

Instantly share code, notes, and snippets.

@twiddles
Created April 14, 2011 12:07
Show Gist options
  • Save twiddles/919341 to your computer and use it in GitHub Desktop.
Save twiddles/919341 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("test")
end
class User
include Mongoid::Document
field :name
embeds_many :user_events
end
class UserEvent
include Mongoid::Document
belongs_to :event
embedded_in :user
field :attending?, :type => Boolean
field :money_spent
end
class Event
include Mongoid::Document
field :title
field :date
end
User.collection.remove
Event.collection.remove
UserEvent.collection.remove
User.create(:name => 'Jimmy')
u = User.create(:name => 'Joe')
e = Event.create(:title => 'Oscars', :date => Time.new)
u.user_events.create(:event => e, :attending? => true)
User.all.each do |user|
user_events = user.user_events.where(:attending? => true)
puts user.name + " isn't going anywhere" if user_events.blank?
user_events.each do |e|
puts user.name + ' is going to the ' + e.event.title
end
end
# Output:
# Jimmy isn't going anywhere
# Joe is going to the Oscars
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment