Closed snyt45 closed 3 years ago
プロジェクトのひな形作成
docker-compose run web rails new . --force --skip-bundle
gemをインストール
docker-compose run web bundle install
webpackerをインストール
docker-compose run --rm web rails webpacker:install
docker-compose up
後、http://localhost:3000/ にアクセス
1章でやったおかげで環境構築がエラーなくできるようになった。
書籍モデル作成
docker-compose run --rm web bin/rails g model Book \
name:string \
published_on:date \
price:integer
マイグレーションの内容をDBへ反映
docker-compose run --rm web bin/rails db:create
docker-compose run --rm web bin/rails db:migrate
書籍のデータを作成する
(1..5).each do |i|
Book.create(
name: "Book #{i}",
published_on: Time.parse("20191224").ago(i.months),
price: (i * 1000),
)
end
出版社、著者モデルを作成
docker-compose run --rm web bin/rails g model Publisher name:string address:text
docker-compose run --rm web bin/rails g model Author name:string penname:string
docker-compose run --rm web bin/rails db:migrate
書籍モデルと出版社モデルの関連付けを確認
docker exec -it (docker-compose ps -q web) /bin/bash # webコンテナに入る
rails c # コンソール開く
book = Book.new(name: "パーフェクトRails")
publisher = book.build_publisher
publisher.assign_attributes(name: "技術評論社")
book.save!
book = Book.find(1)
book.publisher
publisher = Publisher.find(1)
publisher.books
書籍と著者モデルの中間テーブルを作成する
docker-compose run --rm web bin/rails g model BookAuthor book:references author:references
docker-compose run --rm web bin/rails db:migrate
改めてデータを用意しなおそうとリセットして、Book.create(..)
でデータを作ろうとしてもデータが作れない。。
# DB初期化、マイグレーション実行
docker-compose run --rm web bin/rails db:migrate:reset
Author.createはINSERT文が発行される。 Book.createはINSERT文が発行されない…
Book.create!
irb(main):009:0> Book.create!
Traceback (most recent call last):
2: from (irb):9
1: from (irb):9:in `rescue in irb_binding'
ActiveRecord::RecordInvalid (Validation failed: Publisher must exist)
関連付けをした後なので、出版社が必須みたいだ。
データを用意する
# DB初期化、マイグレーション実行
docker-compose run --rm web bin/rails db:migrate:reset
docker exec -it (docker-compose ps -q web) /bin/bash # webコンテナに入る
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)
publisher.save!
# 著者作成
matz = Author.create(name: "Matsumoto Yukihiro", penname: "Matz")
dhh = Author.create(name: "David Heinemeier Hansson", penname: "DHH")
matz.books << Book.find(1)
matz.books << Book.find(2)
matz.reload.books.count # オブジェクトの状態ではなく、DBのデータを引き直す
book = Book.find(1)
book.authors << dhh
book.reload.authors.pluck(:name)
書籍と著者の多対多がテーブル上どうなっているか確認する
docker exec -it (docker-compose ps -q web) /bin/bash # webコンテナに入る
bin/rails db # dbコンソール開く
著者テーブル確認
sqlite> .mode line # DBの出力結果をわかりやすくする
sqlite> select * from authors;
id = 1
name = Matsumoto Yukihiro
penname = Matz
created_at = 2021-07-20 00:23:57.883678
updated_at = 2021-07-20 00:23:57.883678
id = 2
name = David Heinemeier Hansson
penname = DHH
created_at = 2021-07-20 00:24:02.955754
updated_at = 2021-07-20 00:24:02.955754
書籍テーブル確認
sqlite> select * from books;
id = 1
name = Book 1
published_on = 2021-07-20
price = 1000
created_at = 2021-07-20 00:21:07.535169
updated_at = 2021-07-20 00:21:07.535169
publisher_id = 1
id = 2
name = Book 2
published_on = 2021-07-20
price = 2000
created_at = 2021-07-20 00:21:14.516856
updated_at = 2021-07-20 00:21:14.516856
publisher_id = 1
書籍と著者の中間テーブル確認
sqlite> select * from book_authors;
id = 1
book_id = 1
author_id = 1
created_at = 2021-07-20 00:24:07.973617
updated_at = 2021-07-20 00:24:07.973617
id = 2
book_id = 2
author_id = 1
created_at = 2021-07-20 00:24:12.588286
updated_at = 2021-07-20 00:24:12.588286
id = 3
book_id = 1
author_id = 2
created_at = 2021-07-20 00:25:37.429434
updated_at = 2021-07-20 00:25:37.429434
Bookモデルに販売状況カラムの追加
❯ docker-compose run --rm web bin/rails g migration AddStatusToBooks sales_status:integer
Creating perfect-rails2_web_run ... done
invoke active_record
create db/migrate/20210724020924_add_status_to_books.rb
❯ docker-compose run --rm web bin/rails db:migrate
Creating perfect-rails2_web_run ... done
== 20210724020924 AddStatusToBooks: migrating =================================
-- add_column(:books, :sales_status, :integer)
-> 0.0019s
== 20210724020924 AddStatusToBooks: migrated (0.0022s) ========================
Booksコントローラーの作成
❯ docker-compose run --rm web bin/rails g controller books
create app/controllers/books_controller.rb
invoke erb
create app/views/books
invoke test_unit
create test/controllers/books_controller_test.rb
invoke helper
create app/helpers/books_helper.rb
invoke test_unit
invoke assets
invoke scss
create app/assets/stylesheets/books.scss
resources :publishers
で追加されるルーティング
publishers GET /publishers(.:format) publishers#index
POST /publishers(.:format) publishers#create
new_publisher GET /publishers/new(.:format) publishers#new
edit_publisher GET /publishers/:id/edit(.:format) publishers#edit
publisher GET /publishers/:id(.:format) publishers#show
PATCH /publishers/:id(.:format) publishers#update
PUT /publishers/:id(.:format) publishers#update
DELETE /publishers/:id(.:format) publishers#destroy
書籍管理アプリケーションに管理者を登録し、ログインする場合のresourceの例
❯ docker-compose run --rm web bin/rails g model User name:string password:string email:string
invoke active_record
create db/migrate/20210727001059_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
❯ docker-compose run --rm web bin/rails g controller Profiles show edit update
create app/controllers/profiles_controller.rb
route get 'profiles/show'
get 'profiles/edit'
get 'profiles/update'
invoke erb
create app/views/profiles
create app/views/profiles/show.html.erb
create app/views/profiles/edit.html.erb
create app/views/profiles/update.html.erb
invoke test_unit
create test/controllers/profiles_controller_test.rb
invoke helper
create app/helpers/profiles_helper.rb
invoke test_unit
invoke assets
invoke scss
create app/assets/stylesheets/profiles.scss
❯ docker-compose run --rm web bin/rails db:migrate
== 20210727001059 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0018s
== 20210727001059 CreateUsers: migrated (0.0020s) =============================
resource :profile
で追加されるルーティング
new_profile GET /profile/new(.:format) profiles#new
edit_profile GET /profile/edit(.:format) profiles#edit
profile GET /profile(.:format) profiles#show
PATCH /profile(.:format) profiles#update
PUT /profile(.:format) profiles#update
DELETE /profile(.:format) profiles#destroy
POST /profile(.:format) profiles#create
https://gihyo.jp/book/2020/978-4-297-11462-6/support