I have see solutions to this sort of issue with 1:N, but they dont seem to read across to 1:1, this is using MongoDB 1.8, Mongoid 2.0.0.rc.8, Rails 3.0.5
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 
Then I start and Rails Console session and:
irb(main):001:0> c = Coach.new(:name => "Tom")  
=> #<Coach _id: da18348d298ca47ad000001, _type: nil, _id: BSON::ObjectId('4da18348d298ca47ad000001'), name: "Tom", coach_id: nil, assist_id: nil> 
irb(main):002:0> a = Coach.new(:name => "Dick") 
=> #<Coach _id: 4da18352d298ca47ad000002, _type: nil, _id: BSON::ObjectId('4da18352d298ca47ad000002'), name: "Dick", coach_id: nil, assist_id: nil> 
irb(main):003:0> t = Team.new(:name => "Allstars") 
=> #<Team _id: 4da18362d298ca47ad000003, _type: nil, _id: BSON::ObjectId('4da18362d298ca47ad000003'), name: "Allstars"> 
irb(main):005:0> t.coach = c 
NoMethodError: undefined method `constantize' for Coach:Class 
irb(main):005:0> c.coached = t 
NoMethodError: undefined method `constantize' for Team:Class 
any advice would be much appreciated!
You're referencing the class Team when defining Coach, but the class does not yet exist. 
You have two options:
class_name as a String instead of the constant e.g. :class_name => 'Team' (preferred, see gist):class_name => Team option and let Mongoid figure out the correct classes participating in the association. There is one caveat: You will need to make sure that the class Team is declared before the class Coach (the order of loading your source codes now matters, so this solution is not ideal)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With