Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Preserve the order of the mongoid documents in a referenced one-to-many or many-to-many relationship.

License

Notifications You must be signed in to change notification settings

demarque/mongoid-sorted-relations

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Mongoid Sorted Relations Build Status

Problem

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

Install

gem install mongoid-sorted-relations

Rails 3

In your Gemfile:

gem 'mongoid-sorted-relations'

Usage

Model integration

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

  has_many :chapters
  has_and_belongs_to_many :authors
end

Getting the relations in the right order

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

book = Book.new 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"

book.sorted_authors.map(&:name)
#=> ['Sun Tzu', 'Sun Wu', 'Lao Zi']

book.sorted_authors.limit(2).map(&:name)
#=> ['Sun Tzu', 'Sun Wu']

Copyright

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

About

Preserve the order of the mongoid documents in a referenced one-to-many or many-to-many relationship.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages