0
0
Ruby on Railsframework~30 mins

Column types and attributes in Ruby on Rails - Mini Project: Build & Apply

Choose your learning style9 modes available
Column types and attributes
📖 Scenario: You are building a simple Rails application to manage a library's book collection. Each book has a title, author, number of pages, and a flag indicating if it is currently available for borrowing.
🎯 Goal: Create a Rails migration to define a books table with appropriate column types and attributes for the book's title, author, pages, and availability.
📋 What You'll Learn
Create a migration file defining a books table
Add a title column as a string
Add an author column as a string
Add a pages column as an integer
Add an available column as a boolean with a default value of true
💡 Why This Matters
🌍 Real World
Defining database tables with correct column types and attributes is essential for storing and managing data in Rails applications.
💼 Career
Rails developers frequently write migrations to create and modify database schemas, ensuring data integrity and application functionality.
Progress0 / 4 steps
1
Create the books table
Write a migration class called CreateBooks inheriting from ActiveRecord::Migration[7.0]. Inside the change method, create a table named books.
Ruby on Rails
Need a hint?

Use create_table :books do |t| ... end inside the change method.

2
Add title and author columns
Inside the create_table :books block, add a title column and an author column, both with the type string.
Ruby on Rails
Need a hint?

Use t.string :title and t.string :author inside the block.

3
Add pages and available columns
Add a pages column with type integer and an available column with type boolean and a default value of true inside the create_table block.
Ruby on Rails
Need a hint?

Use t.integer :pages and t.boolean :available, default: true.

4
Add timestamps
Add timestamps to the books table inside the create_table block to track creation and update times.
Ruby on Rails
Need a hint?

Use t.timestamps inside the block to add created_at and updated_at columns.