Ruby on Rails - creating a model in ruby on rails - ruby on rails tutorial - rails guides - rails tutorial - ruby rails
Create a model
- To create a model (lets call it User) by running:
$ rails g model User
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
which will generate the file app/models/user.rb:
class User
include Mongoid::Document
end
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
- This is all we 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 we generate it. To add created_at and updated_at to our model, add
include Mongoid::Timestamps
Clicking "Copy Code" button will copy the code into the clipboard - memory. Please paste(Ctrl+V) it in your destination. The code will get pasted. Happy coding from Wikitechy - ruby on rails tutorial - rails guides - ruby rails - rubyonrails - learn ruby on rails - team
Our model underneath include Mongoid::Document like:
include Mongoid::Timestamps