-
Type: Improvement
-
Resolution: Fixed
-
Priority: Minor - P4
-
Affects Version/s: None
If an order is specified in the default scope and in the query, the default scope order is added in front of the query order. The result is somewhat surprising - order in default scope takes precedence:
class Band include Mongoid::Document field :name, type: String field :description, type: String default_scope ->{ order(description: 1) } end Band.create!(name: 'aaa', description: 'z') Band.create!(name: 'bbb', description: 'b') Band.all.to_a => [#<Band _id: 5de6bc3dce4ef31e5e2d1cff, name: "foo", description: nil>, #<Band _id: 5de6bc47ce4ef31e5e2d1d00, name: "bar", description: nil>, #<Band _id: 5de6bd2ace4ef31e5e2d1d02, name: "bbb", description: "b">, #<Band _id: 5de6bd35ce4ef31e5e2d1d04, name: "bbb", description: "b">, #<Band _id: 5de6bd2ace4ef31e5e2d1d01, name: "aaa", description: "z">, #<Band _id: 5de6bd35ce4ef31e5e2d1d03, name: "aaa", description: "z">] Band.order(name: 1).to_a => [#<Band _id: 5de6bc47ce4ef31e5e2d1d00, name: "bar", description: nil>, #<Band _id: 5de6bc3dce4ef31e5e2d1cff, name: "foo", description: nil>, #<Band _id: 5de6bd2ace4ef31e5e2d1d02, name: "bbb", description: "b">, #<Band _id: 5de6bd35ce4ef31e5e2d1d04, name: "bbb", description: "b">, #<Band _id: 5de6bd2ace4ef31e5e2d1d01, name: "aaa", description: "z">, #<Band _id: 5de6bd35ce4ef31e5e2d1d03, name: "aaa", description: "z">] Band.order(name: 1) => #<Mongoid::Criteria selector: {} options: {:sort=>{"description"=>1, "name"=>1}} class: Band embedded: false>
This behavior matches ActiveRecord one and is this way because the default scope (with its order) is evaluated first, with query conditions being evaluated later.
Having the query conditions take precedence is tricky because both default scope and query may have multiple order statements, thus we would need to keep track of "order insertion point". Additionally this would make Mongoid behave contrary to ActiveRecord.
Since the current behavior can be surprising, this ticket is to document it in the tutorial.
- is duplicated by
-
MONGOID-4796 Does Mongoid support order_by?
- Closed
- links to