Book Image

Learning Mongoid

By : Gautam Rege
Book Image

Learning Mongoid

By: Gautam Rege

Overview of this book

Mongoid helps you to leverage the power of schema-less and efficient document-based design, dynamic queries, and atomic modifier operations. Mongoid eases the work of Ruby developers while they are working on complex frameworks. Starting with why and how you should use Mongoid, this book covers the various components of Mongoid. It then delves deeper into the detail of queries and relations, and you will learn some tips and tricks on improving performance. With this book, you will be able to build robust and large-scale web applications with Mongoid and Rails. Starting with the basics, this book introduces you to components such as moped and origin, and how information is managed, learn about the various datatypes, embedded documents, arrays, and hashes. You will learn how a document is stored and manipulated with callbacks, validations, and even atomic updates. This book will then show you the querying mechanism in detail, right from simple to complex queries, and even explains eager loading, lazy evaluation, and chaining of queries. Finally, this book will explain the importance of performance tuning and how to use the right indexes. It also explains MapReduce and the Aggregation Framework.
Table of Contents (14 chapters)
Learning Mongoid
About the Author
About the Reviewers

Acts as state machine (aasm)

A finite-state machine (FSM) is based on the concept of an object being in a particular state and then transitioning to other states based on certain events. Let's take the example of reserving a book. The states that the reservation can be in are available, booked, and late. For this example, the reservation can transition to these states in any of the following ways:

  • All books have a single reservation each

  • The default status of a reservation is available

  • A customer can reserve a book if it's available

  • A customer can return a book if it's booked or it's late

  • A book is delayed if it's reserved for more than the default seven days

To use the state machine, we need to add the aasm gem into Gemfile as follows:

class Reservation
  include Mongoid::Document
  include AASM

  field :aasm_state, type: String
  field :booked_on, type: Date
  field booked_by, type: String
  field :returned_on, type: Date
  field :days_reserved, type: Integer, default: 7
  belongs_to :book...