Rails prototype Post-It: Lesson 1

This is what I have done on week 1, course 2 of Tealeaf Academy:

Database tables - schema view #

Migration files #

$ rails generate migration create_posts
$ rails g migration create_users
$ rails g migration add_user_id_to_posts
$ rails g migration create_comments
$ rails g migration create_categories
$ rails g migration create_post_categories

posts #

create_table
  t.string :url, :title
  t.text :description

users #

  t.string :username

add user_id to posts #

add_column :posts, :user_id, 'integer'

comments #

create_table
  t.text :body
  t.integer :post_id, :user_id

category #

  t.string :name

join table for many to many association of post and category #

  t.integer :post_id, :category_id

for all do not forget #

  t.timestamps

build schema file and database #

$ rake db:migration

Model files #

post #

class Post < ActiveRecord::Base
  belongs_to :creator, foreign_key: 'user_id', class_name: 'User'
  has_many :comments
  has_many :categories, through: :post_categories
  has_many :post_categories
end

user #

  has_many :posts
  has_many :comments

comment #

  belongs_to :post, foreign_key: 'post_id'
  belongs_to :creator, foreign_key: 'user_id', class_name: 'User'

category #

  has_many :posts, through: :post_categories
  has_many :post_categories

post_category #

  belongs_to :post, foreign_key: 'post_id'
  belongs_to :category, foreign_key: 'category_id`

Controller files #

posts #

class PostsController < ApplicationController
  def index
    @posts = Post.all
  end

  def show
    @post = Post.find(params[:id])
  end
end

Routes file #

  resources :posts, except: :destroy
 
1
Kudos
 
1
Kudos

Now read this

레일스로 코딩 배우기

나는 루비가 코딩을 배우기에 좋은 출발점이라고 생각한다 Learn To Program 이라는 책을 읽어보라 https://pine.fm/LearnToProgram/ 물론 어느 programming language 로 시작하든지 코딩을 배우는 것은 인생에서 매우 중요하다고 생각한다 그리고 루비를 배우기에는 레일스가 좋은 출발점이라고 생각한다 지금 시작해보자 rails new app_name cd app_name 레일스의... Continue →