Skip to content

Instantly share code, notes, and snippets.

@kasima
Created January 26, 2011 22:36
Show Gist options
  • Save kasima/797650 to your computer and use it in GitHub Desktop.
Save kasima/797650 to your computer and use it in GitHub Desktop.
1:1 and 1:many to same class
class Game
include Mongoid::Document
field :title
referenced_in :dungeon_master, :class_name => 'User'
references_and_referenced_in :players, :class_name => 'User'
end
class User
include Mongoid::Document
field :name
references_one :game, :inverse_of => :dungeon_master
end
master = User.new("John C. Awesome")
player = User.new("Fred Player")
Game.new(:title => "Lonely Game", :dungeon_master => master)
# no problem
Game.new(:title => "Fun Game", :dungeon_master => master, :players => [player])
# You have a nil object when you didn't expect it!
# You might have expected an instance of Array.
# The error occurred while evaluating nil.push
# /Users/kasima/.rvm/gems/ruby-1.9.2-p136@topaz/gems/mongoid-2.0.0.rc.6/lib/mongoid/relations/bindings/referenced/many_to_many.rb:49:in `bind_one'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment