I have an issue with mongoid inheritance. I have a Zipcode with inherits from Location class both mongoid documents. It has the following simple inheritance structure:
class Zipcode < Location
Also I have an object which fills out the Zipcode object using nested_attributes. I have noticed when it tries to create the Zipcode object it instead creates a Location object instead. In my test code everything works fine but in my production code things do not work properly.
After running ruby-debug over my code I have found the following behavior in the file: ~/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
Breakpoint 2 at /Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
/Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
@_type ||= [descendants + [self]].flatten.uniq.map { |t| t.to_s }
(rdb:48) p descendants
[]
(rdb:48) p Zipcode.ancestors && [Location]
[Location]
(rdb:48) p descendants
[Zipcode]
(rdb:48)
See it’s strange that descendants (which is in BasicObject) gets filled out when Zipcode.ancestors is called. I am using Ruby1.9.2, Mongoid 2.2.3 and Rails3.1.1.
Questions: Is there something special in the mongoid syntax to get inheritance in nested attributes? Is there an issue that needs to be fixed in either mongoid, rails or ruby?
Update:
Just to show that breaking into the test code works the first time:
Breakpoint 1 at /Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
/Users/pc/work/Project/bson/ruby/1.9.1/gems/mongoid-2.2.3/lib/mongoid/document.rb:277
@_type ||= [descendants + [self]].flatten.uniq.map { |t| t.to_s }
(rdb:1) p descendants
[Zipcode]
Found the solution had to change
in config/environments/development.rb. It messes up inheritance in rails sometimes apparently.