Skip to content

Instantly share code, notes, and snippets.

@jingoro
Created May 4, 2012 19:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jingoro/2597258 to your computer and use it in GitHub Desktop.
Save jingoro/2597258 to your computer and use it in GitHub Desktop.
Mongoid Ambigious Relation Bug?
require 'rubygems'
require 'bundler/setup'
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("mongoid-test")
end
class Group
include Mongoid::Document
has_many :employees, class_name: 'Person', foreign_key: :employer_id
has_many :subcontractors, class_name: 'Person', foreign_key: :client_id
end
class Person
include Mongoid::Document
belongs_to :employer, class_name: 'Group'
belongs_to :client, class_name: 'Group' #, inverse_of: :subcontractors
end
a = Group.create!
b = Group.create!
person = Person.new(employer: a, client: b)
p person.employer == person.client # should be false
person.save!
person.reload
# this shows false on mongoid 2.4.8, true on mongoid 2.4.9
p person.employer == person.client # should be false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment