mongoid/mongoid — Issue #1684: Mongoid fails silently when it can't associate a 1..N relationship
Author: cdussud
Created: 2012-02-07 23:17:14+00:00
State: open

I created two 1..N relationships pointing to the same class (this is a made-up contrived example)

class Town
has_many :citizens, class_name: 'Person'
has_many :employees, class_name: 'Person'

class Person
belongs_to :home, class_name: 'Town'
belongs_to :employer, class_name: 'Town'

Here I basically forgot to use inverse_of.

My issue is that Mongoid will never complain if you use these associations. Worse, it almost looks like it works. The in-memory objects work fine when objects are assigned to them , but when they're saved and reloaded the association is empty.

I'm sure this causes subtle, hard-to-find bugs. I only caught this because of a very specific unit test in my code. Mongoid should raise an exception when trying to save an object with an incorrectly-specified association. At minimum it should provide a built-in validator so that I can do this myself.

And minor issue B: the use of inverse_of isn't even documented on 1..N relationships.