-
Type: Bug
-
Resolution: Fixed
-
Priority: Major - P3
-
Affects Version/s: 7.1.1, 7.0.7
-
Component/s: Associations
-
None
-
Environment:System : Linux 5.6.6-arch1-1 #1 SMP PREEMPT Tue, 21 Apr 2020 10:35:16 +0000 x86_64 GNU/Linux
Ruby : ruby 2.6.6p146 (2020-03-31 revision 67876) [x86_64-linux]
Mongodb : v3.4.22
Hello,
First, I'm using mongoid 7.0.7, but I've tested this on mongoid 7.1.1 and the results are the same.
This bug was not present in mongoid 6.2.1 .
About the bug :
When we declare an embeds_one/embeds_many field on a document, and we specify a class_name with a "::" root namespace (ie "::Test::Foobar"), the parent of this embedded document will not be set, and will return nil.
What I found is that the method determine_inverses in Mongoid::Association::Embedded::EmbeddedIn/Mongoid::Association::Embedded::EmbedsMany/Mongoid::Association::Embedded::EmbedsOne have this line : rel.relation_class_name == inverse_class_name
rel.relation_class_name return the value given by the option class_name, the string with the root namespace "::", but inverse_class_name return the class name without any leading "::", because it's simply a call to `name` on the class of the parent.
Here's some code showing this behaviour :
#### Test with embeds_one class Test include Mongoid::Document embeds_one :test_foobar, class_name: "::Test::Foobar" end class Test::Foobar include Mongoid::Document embedded_in :test_parent, class_name: "::Test" # Doesn't work with this line #embedded_in :test_parent, class_name: "Test" # Work with this line end t=Test.new t.test_foobar = Test::Foobar.new # Should NOT be nil t.test_foobar.test_parent #### Test with embeds_many class Test include Mongoid::Document embeds_many :test_foobars, class_name: "::Test::Foobar" end class Test::Foobar include Mongoid::Document embedded_in :test_parent, class_name: "::Test" # Doesn't work with this line #embedded_in :test_parent, class_name: "Test" # Work with this line end t=Test.new f=t.test_foobars.new # Should NOT be nil f.test_parent
- related to
-
MONGOID-4751 Do not use relation_class_name for class comparisons
- Backlog
- links to