Columns store data in a database table. Different types hold different kinds of information.
Column types and attributes in Ruby on Rails
create_table :table_name do |t| t.column_type :column_name, options end
Replace column_type with the type like string, integer, or boolean.
Options can include null: false to require a value or default: value to set a starting value.
t.string :name
# Stores text data like names or titlest.integer :age, null: false, default: 0 # Stores whole numbers, cannot be empty, starts at 0
t.boolean :active, default: true
# Stores true or false values, defaults to truet.datetime :created_at
# Stores date and time informationThis migration creates a users table with columns for username, age, admin status, and join date. It also adds timestamps for created_at and updated_at automatically.
class CreateUsers < ActiveRecord::Migration[7.0] def change create_table :users do |t| t.string :username, null: false t.integer :age, default: 18 t.boolean :admin, default: false t.datetime :joined_at t.timestamps end end end
Always pick the smallest column type that fits your data to save space.
Use null: false to make sure important data is always there.
Default values help avoid errors when no data is given.
Columns define what kind of data a table holds.
Choose types like string, integer, boolean, or datetime based on your data.
Use attributes like null and default to control data rules.
Practice
Solution
Step 1: Understand column types for data
Boolean columns store true or false values, perfect for yes/no data.Step 2: Compare with other types
String and text store text, integer stores numbers, so they don't fit true/false data.Final Answer:
boolean -> Option AQuick Check:
Boolean = true/false [OK]
- Choosing string or text for true/false data
- Using integer to represent boolean values
- Confusing text with boolean type
title with a default value 'Untitled' in a Rails migration?Solution
Step 1: Identify correct column type and options
We want a string type with null: false and default: 'Untitled'.Step 2: Check syntax order and options
add_column :books, :title, :string, null: false, default: 'Untitled' correctly uses :string, null: false, and default: 'Untitled'. The option with null: true allows null values. The option with :text uses the wrong type. The option without default misses the default value.Final Answer:
add_column :books, :title, :string, null: false, default: 'Untitled' -> Option CQuick Check:
Correct syntax with null and default options [OK]
- Forgetting null: false for required columns
- Using wrong column type like text instead of string
- Omitting default value when needed
create_table :users do |t| t.integer :age, null: false, default: 18 t.boolean :active, default: true end
What will be the default value of
active for a new user if not specified?Solution
Step 1: Read the default value for active
The migration sets default: true for the boolean column active.Step 2: Understand default behavior
If no value is given when creating a user, active will be set to true automatically.Final Answer:
true -> Option AQuick Check:
Boolean default true means new records get true [OK]
- Assuming default is false if not specified
- Confusing nil with default value
- Thinking integer 0 is default for boolean
add_column :orders, :price, :integer, null: true, default: 0
But you want to ensure price is always set and never null. What is the fix?
Solution
Step 1: Identify null constraint
Currently null: true allows price to be null, which is not desired.Step 2: Fix null constraint
Changing null: true to null: false ensures price cannot be null.Final Answer:
Change null: true to null: false -> Option BQuick Check:
Use null: false to prevent null values [OK]
- Leaving null: true when value must be required
- Removing default without fixing null
- Changing type without addressing null
status column to your tasks table that stores short text but limits it to 10 characters, defaults to 'pending', and cannot be null. Which migration line is correct?Solution
Step 1: Choose correct column type and limit
Use :string with limit: 10 to restrict text length to 10 characters.Step 2: Set null and default options
Set null: false to require a value and default: 'pending' for initial value.Final Answer:
add_column :tasks, :status, :string, limit: 10, null: false, default: 'pending' -> Option DQuick Check:
String with limit, null false, and default set [OK]
- Using text type which ignores limit
- Allowing null when value is required
- Setting wrong limit size
Ready to prove it?
Pass the test and this pattern moves unknown
