Try   HackMD

パRails輪読会🚂更新手順・空のリポジトリ作成

  • わからなくなりそうなのでまとめておきます。

輪読会のはじめにすること

  1. 現在のブランチ確認
$ git branch
* main
  1. 最新の内容をpullする
$ git pull origin main
  1. 更新する日付でブランチを切る
$ git switch -c 20230830

輪読会の終わりにすること

【 VSCode側 】

  1. 更新を確認。
$ git status
  1. 変更を登録する。
$ git add .
  1. コミットメッセージを記入。
$ git commit -m "Scopeの定義"
  1. 現在のブランチ確認。
$ git branch
* 20230830
  main
  1. 変更をpushする。
$ git push origin 20230830 または git push origin @

【 GitHub側 】

  • PRが作成されていることを確認する。
    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

【 HackMD 】
ノートの読んだところにPRのURLを追記しておく。

データ作成

  1. マイグレーションの内容をDBに反映させる。
$ bin/rails db:migrate

2-2-1 書籍のデータを作る。

(1..5).each do |i|
Book.create(
name: "Book #{i}",
published_on: Time.parse("20191224").ago(i.months),
price: (i * 1000),
)
end

輪読会内で使ったコード

2章 リスト2.3 app/models/book.rbにcostlyスコープを追加する
class Book < ApplicationRecord
scope :costly, -> { where("price > ?", 3000) }
end

リスト2.4 app/models/book.rbにwritten_aboutスコープを追加する

class Book < ApplicationRecord
scope :costly, -> { where("price > ?", 3000) }
scope :written_about, ->(theme) { where("name like ?","%#{theme}%") }
scope :find_price, ->(price) { find_by(price: price) }
end

リスト2.5 db/migrate/20200514152514_add_publisher_id_to_books.rb

class AddPublisherIdToBooks < ActiveRecord::Migration[6.0]
  def change
    add_reference :books, :publisher, foreign_key: true
  end
end

リスト2.6

class AddPublisherIdToBooks < ActiveRecord::Migration[6.0]
  def change
    add_reference :books, :publisher, foreign_key: true
    change_column :books, :publisher_id, :integer, :null: false
  end
end

リスト2.7

class Book < ApplicationRecord
    has_many :books
end

リスト2.8

class Book < ApplicationRecord
scope :costly, -> { where("price > ?", 3000) }
scope :written_about, ->(theme) { where("name like ?","%#{theme}%") }

belongs_to :publisher
end
$ rails c
publisher = Publisher.create(
    name: "Gihyo inc.",
    address: "Ichigaya",
)
publisher.books << Book.create(
    name: "Book 1",
    published_on: Time.current,
    price: 1000,
)
publisher.books << Book.create(
    name: "Book 2",
    published_on: Time.current,
    price: 2000,
)

リスト2.10のあとのrails cのコード
(データを作る部分だけ抜粋)

$ bin/rails c
matz = Author.create(name: "Matsumoto Yukihiro", penname: "Matz")
dhh = Author.create(name: "David Heinemeier Hannson", penname: "DHH")
matz.books << Book.find(1)
matz.books << Book.find(2)
book = Book.find(1)
book.authors << dhh

2-2-3

$ bin/rails c
Book.create(
  name: "RailsBook",
  published_on: Time.parse("20191224").ago(2.months),
  price: 2980,
  publisher: Publisher.find(1),
)

空のリポジトリ作成

【 GitHub側 】

  1. Github上のNewアイコンをクリックして空のリポジトリ作成。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  2. リポジトリ名、Descriptionを記入。
    (例:Perfect_Ruby_on_Rails_Ch2)、(例:パーフェクトRuby on Rails 輪読会 2023 2章用)

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

【 VSCode側 】

  1. ローカルリポジトリにクローンする。
$ git clone https://github.com/KMZ0209/Perfect_Ruby_on_Rails_Ch2.git
  1. デフォルトのブランチがmasterなのでmainに変更する。
$ git switch -c main
  1. .gitignoreファイルを作成する

    .gitignoreファイルの内容
    ​​​​​​​ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
    ​​​​​​​ #
    ​​​​​​​ # If you find yourself ignoring temporary files generated by your text editor
    ​​​​​​​ # or operating system, you probably want to add a global ignore instead:
    ​​​​​​​ #   git config --global core.excludesfile '~/.gitignore_global'
    
    
    ​​​​​​​ # Ignore bundler config.
    ​​​​​​​ .bundle
    
    ​​​​​​​ # Ignore the default SQLite database.
    ​​​​​​​ /*/db/*.sqlite3
    ​​​​​​​ /*/db/*.sqlite3-journal
    ​​​​​​​ /*/db/*.sqlite3-*
    
    ​​​​​​​ # Ignore all logfiles and tempfiles.
    ​​​​​​​ /*/log/*
    ​​​​​​​ /*/tmp/*
    ​​​​​​​ !/*/log/.keep
    ​​​​​​​ !/*/tmp/.keep
    
    ​​​​​​​ # Ignore pidfiles, but keep the directory.
    ​​​​​​​ /*/tmp/pids/*
    ​​​​​​​ !/*/tmp/pids/
    ​​​​​​​ !/*/tmp/pids/.keep
    
    ​​​​​​​ # Ignore uploaded files in development.
    ​​​​​​​ /*/storage/*
    ​​​​​​​ !/*/storage/.keep
    
    ​​​​​​​ /*/public/assets
    ​​​​​​​ .byebug_history
    
    ​​​​​​​ # Ignore master key for decrypting credentials and more.
    ​​​​​​​ /*/config/master.key
    
    ​​​​​​​ /*/public/packs
    ​​​​​​​ /*/public/packs-test
    ​​​​​​​ node_modules
    ​​​​​​​ yarn-error.log
    ​​​​​​​ yarn-debug.log*
    ​​​​​​​ .yarn-integrity
    
  2. 編集していく(ローカルでrails new してbook_adminを作成するなど)。
    (rails newする手順はこちら→環境構築 2023年版 - HackMD)

$ rails new book_admin --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-text --skip-active-storage --skip-action-cable
  1. 変更を登録
$ git add .
  1. コミットメッセージを書く
$ git commit -m 'rails new'
  1. 新規作成したリポジトリにpushする。
$ git push origin https://github.com/KMZ0209/Perfect_Ruby_on_Rails_Ch2

複数人で編集

【 GitHub側 】

  • ヘッダーメニューからSettingsを選択。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • 左側のメニューでCollaboratorsを選択。

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

  • Add peopleボタンから更新してくれる方を招待していく

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →

    Image Not Showing Possible Reasons
    • The image was uploaded to a note which you don't have access to
    • The note which the image was originally uploaded to has been deleted
    Learn More →