# パRails輪読会🚂更新手順・空のリポジトリ作成 - わからなくなりそうなのでまとめておきます。 ## 輪読会のはじめにすること 1. 現在のブランチ確認 ``` $ git branch * main ``` 2. 最新の内容をpullする ``` $ git pull origin main ``` 3. 更新する日付でブランチを切る ``` $ git switch -c 20230830 ``` ## 輪読会の終わりにすること 【 VSCode側 】 1. 更新を確認。 ``` $ git status ``` 2. 変更を登録する。 ``` $ git add . ``` 3. コミットメッセージを記入。 ``` $ git commit -m "Scopeの定義" ``` 4. 現在のブランチ確認。 ``` $ git branch * 20230830 main ``` 4. 変更をpushする。 ``` $ git push origin 20230830 または git push origin @ ``` 【 GitHub側 】 - PRが作成されていることを確認する。 ![](https://hackmd.io/_uploads/BytLVO2ah.png) 【 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 ``` ## 輪読会内で使ったコード <details> <summary>2章</summary> リスト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のコード (データを作る部分だけ抜粋) ```ruby $ 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 ```ruby $ bin/rails c Book.create( name: "RailsBook", published_on: Time.parse("20191224").ago(2.months), price: 2980, publisher: Publisher.find(1), ) ``` </details> # 空のリポジトリ作成 【 GitHub側 】 1. Github上の`Newアイコン`をクリックして空のリポジトリ作成。 ![](https://hackmd.io/_uploads/BJQFX1na2.png) 2. リポジトリ名、Descriptionを記入。 (例:Perfect_Ruby_on_Rails_Ch2)、(例:パーフェクトRuby on Rails 輪読会 2023 2章用) ![](https://hackmd.io/_uploads/rJeMEyh63.png) 【 VSCode側 】 1. ローカルリポジトリにクローンする。 ``` $ git clone https://github.com/KMZ0209/Perfect_Ruby_on_Rails_Ch2.git ``` 2. デフォルトのブランチがmasterなのでmainに変更する。 ``` $ git switch -c main ``` 3. `.gitignore`ファイルを作成する <details> <summary>.gitignoreファイルの内容</summary> # 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 </details> 4. 編集していく(ローカルでrails new してbook_adminを作成するなど)。 (`rails new`する手順はこちら→[環境構築 2023年版 \- HackMD](https://hackmd.io/3_lnn8_QRD6wEjwFbWcBzQ?view#rails-new%E3%81%99%E3%82%8B%E9%9A%9B%E3%81%AE%E6%89%8B%E9%A0%86%E4%B8%80%E8%A6%A7)) ``` $ rails new book_admin --skip-action-mailer --skip-action-mailbox --skip-action-text --skip-active-text --skip-active-storage --skip-action-cable ``` 5. 変更を登録 ``` $ git add . ``` 6. コミットメッセージを書く ``` $ git commit -m 'rails new' ``` 7. 新規作成したリポジトリにpushする。 ``` $ git push origin https://github.com/KMZ0209/Perfect_Ruby_on_Rails_Ch2 ``` # 複数人で編集 【 GitHub側 】 - ヘッダーメニューからSettingsを選択。 ![image](https://hackmd.io/_uploads/rJomMZPHT.png) - 左側のメニューでCollaboratorsを選択。 ![image](https://hackmd.io/_uploads/HkwiGZPra.png) - Add peopleボタンから更新してくれる方を招待していく ![image](https://hackmd.io/_uploads/H1glQbwHT.png) ![image](https://hackmd.io/_uploads/HJRo7-vBT.png)