Model generation creates a blueprint for your data in a Rails app. It helps you organize and store information easily.
Model generation in Ruby on Rails
rails generate model ModelName field1:type field2:type
Replace ModelName with the name of your data model, usually singular and capitalized.
Specify fields with their data types, like name:string or age:integer.
rails generate model User name:string email:string
rails generate model Product title:string price:decimal
rails generate model Article title:string body:text published:boolean
This example shows how to generate a Book model with three fields. After running the command, Rails prepares the code and database changes needed. Running rails db:migrate applies the changes to your database.
rails generate model Book title:string author:string pages:integer # After running this command, Rails creates: # - A Book model file # - A migration file to create books table with title, author, and pages columns # To apply the migration and create the table, run: rails db:migrate
Model names should be singular and capitalized, like User or Book.
Data types include string, integer, text, boolean, decimal, and more.
Always run rails db:migrate after generating a model to update the database.
Model generation creates a data blueprint and database setup in Rails.
Use rails generate model with fields and types to define your data.
Run migrations to apply changes to your database after generating models.