Skip to content

Instantly share code, notes, and snippets.

@twiddles
Created April 14, 2011 09:15
Show Gist options
  • Save twiddles/919170 to your computer and use it in GitHub Desktop.
Save twiddles/919170 to your computer and use it in GitHub Desktop.
tested on jruby 1.6
require 'rubygems'
require 'mongoid'
class Coach
include Mongoid::Document
field :name, :type => String
belongs_to :coached, :class_name => 'Team', :inverse_of => :coach, :foreign_key => "coach_id"
belongs_to :assisted, :class_name => 'Team', :inverse_of => :assist, :foreign_key => "assist_id"
end
class Team
include Mongoid::Document
field :name, :type => String
has_one :coach, :class_name => 'Coach', :inverse_of => :coached
has_one :assist, :class_name => 'Coach', :inverse_of => :assisted
end
c = Coach.new(:name => "Tom")
a = Coach.new(:name => "Dick")
t = Team.new(:name => "Allstars")
t.coach = c
t.assist = a
c.coached = t
p c
p c.coached.assist.assisted
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment