Tutorial by Examples

First, add Mongoid to your Gemfile: gem "mongoid", "~> 4.0.0" and then run bundle install. Or just run: $ gem install mongoid After installation, run the generator to create the config file: $ rails g mongoid:config which will create the file (myapp)/config/mongoid...
Create a model (lets call it User) by running: $ rails g model User which will generate the file app/models/user.rb: class User include Mongoid::Document end This is all you need to have a model (albeit nothing but an id field). Unlike ActiveRecord, there is no migration files. All the...
As per the Mongoid Documentation, there are 16 valid field types: Array BigDecimal Boolean Date DateTime Float Hash Integer BSON::ObjectId BSON::Binary Range Regexp String Symbol Time TimeWithZone To add a field (let's call it name and have it be a String), add this to your mode...
Mongoid allows the classic ActiveRecord associations: One-to-one: has_one / belongs_to One-to-many: has_many / belongs_to Many-to-many: has_and_belongs_to_many To add an association (lets say the User has_many posts), you can add this to your User model file: has_many :posts and this to ...
Mongoid allows Embedded Associations: One-to-one: embeds_one / embedded_in One-to-many: embeds_many / embedded_in To add an association (lets say the User embeds_many addresses), add this to your User file: embeds_many :addresses and this to your Address model file: embedded_in :user ...
Mongoid tries to have similar syntax to ActiveRecord when it can. It supports these calls (and many more) User.first #Gets first user from the database User.count #Gets the count of all users from the database User.find(params[:id]) #Returns the user with the id found in params[:id] User.w...

Page 1 of 1