2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rails6 のちょい足しな新機能を試す50(scaffold 編)

Posted at

はじめに

Rails 6 に追加されそうな新機能を試す第50段。 今回は、 scaffold 編です。
Rails 6 では、 belongs_to な関係のあるモデルがあるアプリを scaffold で作った場合に、 belongs_to で指定される親モデルの表示のされ方が変わりました。
...ってなんだか良くわかりませんね。

実際にやってみた方がわかりやすいです。

Ruby 2.6.3, Rails 6.0.0.rc1, Rails 5.2.3 で確認しました。Rails 6.0.0.rc1 は gem install rails --prerelease でインストールできます。

$ rails --version
Rails 6.0.0.rc1

rails プロジェクトを作る

$ rails new rails6_0_0rc1
$ cd rails6_0_0rc1

scaffold を使う

scaffold で Author と Book の CRUD を作成します。Book は、 Author に属します。

$ bin/rails g scaffold Author name
$ bin/rails g scaffold Book title author:references

seed データを作る

Author と Book のデータを1つずつ作成します。

db/seeds.rb
author = Author.create(name: 'Dave Thomas')
Book.create(title: 'Programming Ruby', author: author)

データベースを作る

データベースを作って seed データを登録します。

$ bin/rails db:create db:migrate db:seed

rails server を実行して Book のデータを表示する

rails server を実行します。

$ bin/rails s

http://localhost:3000/bookshttp://localhost:3000/books/1 で一覧画面と詳細画面を表示します。

Rails 5 では

一覧画面でも詳細画面でも、Author はAuthorオブジェクト( Author#to_s )を表示します。

一覧画面

rails5index.png

詳細画面

rails5show.png

Rails 6 では

一覧画面でも詳細画面でも、Author は ID ( Author#id ) の 1 を表示します。

一覧画面

rails6index.png

詳細画面

rails6show.png

試したソース

試したソースは以下にあります。
https://github.com/suketa/rails6_0_0rc1/tree/try050_scaffold

参考情報

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?