-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
Affects Version/s: 5.0.0
-
Component/s: None
-
None
-
Environment:ruby 2.2.1, mongoid gem checked out on sha af6f359
Mongoid version : 5.0.0, Ruby: 2.2.1, git sha : af6f359
When a document from a class defined with a has_many relation is loaded, #last on the relation will return the first document.
This spec should pass but it fails
require "spec_helper" class Me include Mongoid::Document has_many :drugs, dependent: :delete, validate: false end class Drug include Mongoid::Document belongs_to :me, counter_cache: true end describe Mongoid::Relations::Referenced::Many do describe "#last" do context "when a base document is loaded from database" do before do person = Me.create! person.drugs.create! person.drugs.create! end it "returns the expected last document" do person = Me.first expect(person.drugs.size).to eq(2) expect(person.drugs.last.id).to_not eq(person.drugs.first.id) end end end end
The problem seems to be into Enumarable#last method, which look like at _added first and do not provide order to _unloaded (see comments on Criteria#last)
- is related to
-
MONGOID-3768 Default $orderby _id:1 on where() causing HUGE performance issues
- Closed
- related to
-
MONGOID-1336 .last/.first should not add _id: 1 sorting in some cases
- Closed
-
MONGOID-1739 .first and .last add _id field to sort resulting in index miss
- Closed