Details
-
Task
-
Status: Closed
-
Major - P3
-
Resolution: Cannot Reproduce
-
None
-
None
Description
I'm working on a DNS management API and I run in a strange issue with polymorphic embedding.
I have these models:
class DNS::Zone
|
include Mongoid::Document
|
embeds_many :rrsets, class_name: 'DNS::RRSet', inverse_of: :zone
|
embeds_one :soa, class_name: 'DNS::Record', as: :container
|
[...]
|
|
class DNS::RRSet
|
include Mongoid::Document
|
embedded_in :zone, class_name: 'DNS::Zone', inverse_of: :rrsets
|
embeds_many :records, class_name: 'DNS::Record', as: :container
|
[...]
|
|
class DNS::Record
|
include Mongoid::Document
|
embedded_in :container, polymorphic: true
|
[...]
|
So I have one record embedded in the Zone and many records embedded in RRSet.
When assiging the soa record to the zone, everything works fine, but when trying to replace it, or just delete it, I get this:
NoMethodError: undefined method `records' for #<DNS::Zone:0x00000009bbf298>
|
from /usr/local/rvm/gems/ruby-2.1.5/bundler/gems/mongoid-8f22ade5fda8/lib/mongoid/traversable.rb:109:in `remove_child'
|
This records name is provided by #metadata_name, which returns :records instead of :soa.
Now the strange part: In its code, #metadata_name just returns __metadata.name but:
z.soa.metadata_name
|
=> :records
|
z.soa.__metadata.name
|
=> :soa
|
And when adding puts __metadata.inspect in #metadata_name, I get this:
z.soa.metadata_name
|
#<Mongoid::Relations::Metadata
|
autobuild: false
|
class_name: DNS::Record
|
cyclic: nil
|
counter_cache:false
|
dependent: nil
|
inverse_of: nil
|
key: records
|
macro: embeds_many
|
name: records
|
order: nil
|
polymorphic: true
|
relation: Mongoid::Relations::Embedded::Many
|
setter: records=>
|
=> :records
|
puts z.soa.__metadata.inspect
|
#<Mongoid::Relations::Metadata
|
autobuild: false
|
class_name: DNS::Record
|
cyclic: nil
|
counter_cache:false
|
dependent: nil
|
inverse_of: :container
|
key: soa
|
macro: embeds_one
|
name: soa
|
order: nil
|
polymorphic: true
|
relation: Mongoid::Relations::Embedded::One
|
setter: soa=>
|
# metadata_name leads to the wrong relation. When can this wrong metadata object come from ?