Skip to content

Latest commit

 

History

History
52 lines (38 loc) · 920 Bytes

Rails_Create-Schema.md

File metadata and controls

52 lines (38 loc) · 920 Bytes

Rails: Create Schema

Create Models

Exclude columns that would be Foriegn Keys

# Example: Table Page with column "title"
rails g model Page title:string
rails g model Category name:string

Generate Associations

Example: Pages can belong to a category.

Page association In your models/page.rb file, add association

class Page < ApplicationRecord
  belongs_to :category
end

Category association In your models/category.rb file, add association

class Category < ApplicationRecord
  has_many :pages
end

Create migrations with associations

rails g migration AddCategoryRefToPages category:references

Scaffold for Models

Scaffold controller and views for models

rails g scaffold_controller Page title category_id
rails g scaffold_controller Category name

Migrate Database to implement changes

rails db:migrate