Ruby on Rails Creating a Model


Example

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 database information for the model is contained in the model file.

Timestamps are not automatically included in your model when you generate it. To add created_at and updated_at to your model, add

include Mongoid::Timestamps

to your model underneath include Mongoid::Document like so:

class User
  include Mongoid::Document
  include Mongoid::Timestamps

end