-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
Labels:None
I think it's reasonable to expect that the order of items inserted in a has_many relationship is the same as the order of items retrieved unless you sort them explicitly. But maybe I am wrong - certainly not a great practice to rely on this. It does look like a regression from a previous version though, I believe this used to work in 2.4.8, although I didn't test.
ruby require 'spec_helper' class Gadget include Mongoid::Document has_many :widgets end class Widget include Mongoid::Document belongs_to :gadget end describe "insert order" do it "works" do widget1 = Widget.create! widget2 = Widget.create! widget3 = Widget.create! gadget = Gadget.create! gadget.widgets << widget2 gadget.widgets << widget1 gadget.widgets << widget3 gadget.widgets.should == [ widget2, widget1, widget3 ] Gadget.find(gadget.id).widgets.should == [ widget2, widget1, widget3 ] # fails end end
The Mongo query executed is a {{find({:_id=>{"$in"=>[ ... }} and the IDs look sorted in ascending order, which would be the problem.
This is in 2.4.10.