Code Monkey home page Code Monkey logo

mongoid-sorted-relations's Introduction

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.

mongoid-sorted-relations's People

Contributors

jhirn avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.