Railsメモ(2) : モデルを作成する
ひな形の作成は完了したので、次に曲のモデルを作成する。
曲の属性としては下記項目を保存することにする。
アーティスト名は別モデルとして作成予定だが、最近の曲はコラボが多いので"featuring"や"and"などを含んだ表示用としてのアーティスト名を保存しておく。
- 曲名
- アーティスト名(表示用)
- ランキング
- 年
Songモデルを作成するために下記コマンドを実行。
$ ./bin/rails g model Song title:string display_artist:string ranking:integer year:integer invoke active_record create db/migrate/20150803142639_create_songs.rb create app/models/song.rb
次にマイグレーションの内容をDBに反映させるために下記コマンドを実行。
$ ./bin/rake db:migrate == 20150803142639 CreateSongs: migrating ====================================== -- create_table(:songs) -> 0.0022s == 20150803142639 CreateSongs: migrated (0.0023s) =============================
rails consoleを使って試しに3曲のデータを追加してみる。
$ ./bin/rails c Loading development environment (Rails 4.2.3) irb(main):001:0> Song.all Song Load (1.8ms) SELECT "songs".* FROM "songs" => #<ActiveRecord::Relation []> irb(main):002:0> Song.create title: 'Happy', display_artist: 'Pharrell Williams', ranking: 1, year: 2014 (0.1ms) begin transaction SQL (0.4ms) INSERT INTO "songs" ("title", "display_artist", "ranking", "year", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["title", "Happy"], ["display_artist", "Pharrell Williams"], ["ranking", 1], ["year", 2014], ["created_at", "2015-08-04 11:34:44.627823"], ["updated_at", "2015-08-04 11:34:44.627823"]] (13.9ms) commit transaction => #<Song id: 1, title: "Happy", display_artist: "Pharrell Williams", ranking: 1, year: 2014, created_at: "2015-08-04 11:34:44", updated_at: "2015-08-04 11:34:44"> irb(main):003:0> Song.create title: 'Dark Horse', display_artist: 'Katy Perry featuring Juicy J', ranking: 2, year: 2014 (0.1ms) begin transaction SQL (0.8ms) INSERT INTO "songs" ("title", "display_artist", "ranking", "year", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["title", "Dark Horse"], ["display_artist", "Katy Perry featuring Juicy J"], ["ranking", 2], ["year", 2014], ["created_at", "2015-08-04 11:35:38.032988"], ["updated_at", "2015-08-04 11:35:38.032988"]] (12.2ms) commit transaction => #<Song id: 2, title: "Dark Horse", display_artist: "Katy Perry featuring Juicy J", ranking: 2, year: 2014, created_at: "2015-08-04 11:35:38", updated_at: "2015-08-04 11:35:38"> irb(main):004:0> Song.create title: 'All of Me', display_artist: 'John Legend', ranking: 3, year: 2014 (0.1ms) begin transaction SQL (0.8ms) INSERT INTO "songs" ("title", "display_artist", "ranking", "year", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["title", "All of Me"], ["display_artist", "John Legend"], ["ranking", 3], ["year", 2014], ["created_at", "2015-08-04 11:36:15.782787"], ["updated_at", "2015-08-04 11:36:15.782787"]] (7.9ms) commit transaction => #<Song id: 3, title: "All of Me", display_artist: "John Legend", ranking: 3, year: 2014, created_at: "2015-08-04 11:36:15", updated_at: "2015-08-04 11:36:15"> irb(main):005:0> Song.all Song Load (0.3ms) SELECT "songs".* FROM "songs" => #<ActiveRecord::Relation [#<Song id: 1, title: "Happy", display_artist: "Pharrell Williams", ranking: 1, year: 2014, created_at: "2015-08-04 11:34:44", updated_at: "2015-08-04 11:34:44">, #<Song id: 2, title: "Dark Horse", display_artist: "Katy Perry featuring Juicy J", ranking: 2, year: 2014, created_at: "2015-08-04 11:35:38", updated_at: "2015-08-04 11:35:38">, #<Song id: 3, title: "All of Me", display_artist: "John Legend", ranking: 3, year: 2014, created_at: "2015-08-04 11:36:15", updated_at: "2015-08-04 11:36:15">]>
これで曲のモデルにデータを追加できることを確認したが、
rails consoleが見にくいので次のステップで見やすくしてみる。
- 作者: すがわらまさのり,前島真一,近藤宇智朗,橋立友宏
- 出版社/メーカー: 技術評論社
- 発売日: 2014/06/06
- メディア: 大型本
- この商品を含むブログ (8件) を見る