Preserve the order of the mongoid documents in a referenced one-to-many or many-to-many relationship.
Switch branches/tags
Nothing to show
Clone or download
fbernier mend
Fix build
Latest commit 912c905 Dec 4, 2012
Failed to load latest commit information.
lib Fixes for mongoid3 Oct 19, 2012
spec Fixes for mongoid3 Oct 19, 2012
.gitignore Initial commit May 15, 2012
.travis.yml fix build Dec 4, 2012
Gemfile Initial commit May 15, 2012
LICENSE Initial commit May 15, 2012 Simplify initialization process May 15, 2012
Rakefile Initial commit May 15, 2012
mongoid-sorted-relations.gemspec mend Dec 4, 2012

Mongoid Sorted Relations Build Status


In Mongoid, the HABTM relations return docs in the wrong order. This gem will give your document the ability to retrieve it's relations in the same order it was placed in.

For more details about this issue, see #1548


gem install mongoid-sorted-relations

Rails 3

In your Gemfile:

gem 'mongoid-sorted-relations'


Model integration

class Book
  include Mongoid::Document
  include Mongoid::SortedRelations

  has_many :chapters
  has_and_belongs_to_many :authors

Getting the relations in the right order

Mongoid::SortedRelations will add a sorted_ methods to all the has_many and habtm relationships.

book = title: 'The Art of War'
book.authors << Author.create name: "Sun Tzu"
book.authors << Author.create name: "Sun Wu"
book.authors << Author.create name: "Lao Zi"
#=> ['Sun Tzu', 'Sun Wu', 'Lao Zi']

#=> ['Sun Tzu', 'Sun Wu']


Copyright (c) 2012 De Marque inc. See LICENSE for further details.