Created: 2012-03-22 22:24
Updated: 2016-06-07 06:46


ActsAsList for Mongoid


Mongoid::ActsAsList provides the ability of ordering and sorting a number of objects in a list using Mongoid as an ODM.

Build Status Dependency Status


Place the following in your Gemfile:

gem 'mongoid_acts_as_list', '~> 0.0.5'

Then run bundle install


Configure defaults values used by ActsAsList:

Mongoid::ActsAsList.configure do |config|

  # These are the default values. Modify as you see fit:

  config.default_position_field = :position
  config.start_list_at = 0


Make sure it is loaded before calling acts_as_list. You can place this code in an initializer file for example.


Activate ActsAsList in your models.

In has_many/belongs_to associations, you will need to provide a :scope option:

class List
  include Mongoid::Document

  has_many :items

class Item
  include Mongoid::Document
  include Mongoid::ActsAsList

  belongs_to :list
  acts_as_list scope: :list

On embedded document, the scope option is not necessary:

class List
  include Mongoid::Document

  embeds_many :items

class Item
  include Mongoid::Document
  include Mongoid::ActsAsList

  embedded_in :list

The public API is composed of the following methods. All of the API from the original ActiveRecord ActsAsList gem is also available. Check the source and documentation to find out more!

## Class Methods

list.items.order_by_position #=> returns all items in `list` ordered by position

## Instance Methods

item.move to: 2              #=> moves item to position number 2
item.move to: :start         #=> moves item to the first position in the list
item.move to: :end           #=> moves item to the last position in the list
item.move before: other_item #=> moves item before other_item
item.move after:  other_item #=> moves item after other_item
item.move forward:  3        #=> move item 3 positions closer to the end of the list
item.move backward: 2        #=> move item 2 positions closer to the start of the list
item.move :forward           #=> same as item.move(forward:  1)
item.move :backward          #=> same as item.move(backward: 1)

item.in_list?                #=> true
item.remove_from_list        #=> sets the position to nil and reorders other items
item.not_in_list?            #=> true


item.next_item               #=> returns the item immediately following `item` in the list
item.previous_item           #=> returns the item immediately preceding `item` in the list

## Original ActsAsList #move_ methods

item.move_higher             #=> same as item.move(:backward)
item.move_lower              #=> same as item.move(:forward)
item.move_to_top             #=> same as item.move(to: :start)
item.move_to_bottom          #=> same as item.move(to: :end)
item.increment_position      #=> increments the position number without affecting other items
item.decrement_position      #=> decrements the position number without affecting other items
item.insert_at 3             #=> same as item.move(to: 3)


Tested with Mongoid 2.4.6 on Ruby 1.9.3-p125, Rails 3.2.2, and Mongo 2.x


  • Open up an issue on Github
  • Fork the project
  • Start a feature/bugfix branch
  • Start writing tests
  • Commit and push until all tests are green and you are happy with your contribution
  • Send a pull request mentionning the issue number (i.e. "Fixes #123")


Copyright (c) 2012 Olivier Melcher, released under the MIT license

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more