Book Image

Ruby and MongoDB Web Development Beginner's Guide

By : Gautam Rege
Book Image

Ruby and MongoDB Web Development Beginner's Guide

By: Gautam Rege

Overview of this book

<p>MongoDB is a high-performance, open source, schema-free document-oriented database. Ruby is an object- oriented scripting language. Ruby and MongoDB are an ideal partnership for building scalable web applications.<br /><br /><em>Ruby and MongoDB Web Development Beginner's Guide</em> is a fast-paced, hands-on guide to get started with web application development using Ruby and MongoDB. The book follows a practical approach, using clear and step-by-step instructions and examples in Ruby to demonstrate application development using MongoDB. <br /><br />The book starts by introducing the concepts of MongoDB. The book teaches everything right from the installation to creating objects, MongoDB internals, queries and Ruby Data Mappers. <br /><br />You will learn how to use various Ruby data mappers like Mongoid and MongoMapper to map Ruby objects to MongoDB documents.<br /><br />You will learn MongoDB features and deal with geo-spatial indexing with MongoDB and Scaling MongoDB. <br /><br />With its coverage of concepts and practical examples, <em>Ruby and MongoDB Web Development Beginner's Guide</em> is the right choice for Ruby developers to get started with developing websites with MongoDB as the database.</p>
Table of Contents (18 chapters)
Ruby and MongoDB Web Development Beginner's Guide
Credits
About the Author
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface

Defining attributes in models


Until now we have seen how attributes are added in models. But we never really dug deeper to find out how that works.

A typical model looks like the following:

class Book
include Mongoid::Document
field :title, type: String
field :publisher, type: String
field :published_on, type: Date
field :votes, type: Array
field :reviews, type: Hash
end

The field method from Mongoid::Document takes at least one mandatory parameter and some optional arguments—name is mandatory and here are some optional arguments. The ones we would use most are :type and :default. The optional arguments are explained as follows:

  • :type: It is the data type which should either be a String, Data, Integer, Float, Bignum, Boolean, or something similar

  • :as: This is required when specifying a polymorphic relation

  • :default: This sets the default value to the field

  • :localize: It tells Mongoid that this is i18n compliant

  • :identity: This is for specifying the information for the identity map...