【Ruby on Rails】モデルの作成
またドットインストールを進めていきますよと。
#05 タスク管理アプリを作ってみよう | Ruby on Rails 4入門 - プログラミングならドットインストール
TaskAppは
Projectがあって、その中でTaskがある、といった形で。
rails newしてモデルを作成
一度作って必要なバンドルはインストールしてるはずなのでスキップ(その後怒られましたが)
$ rails new taskapp --skip-bundle $ cd taskapp/ $ vi Gemfile
dotinstallではそのままrails s
でいけてるが、怒られたので
$ rails s Could not find gem 'therubyracer (>= 0) ruby' in the gems available on this machine. Run `bundle install` to install missing gems.
therubyracerのコメントをはずす。
そしてbundle install
rails generate model Project title:string rails g model Project title
generate
はg
、タイトルはstringがデフォなので省略可。
モデル名は単数形、頭文字は大文字で
マイグレイト
rake db:migrate
#06 rails db/rails consoleを使おう | Ruby on Rails 4入門 - プログラミングならドットインストール
DBの確認
$ rails db SQLite version 3.8.2 2013-12-06 14:53:30 Enter ".help" for instructions Enter SQL statements terminated with a ";"
sqlite> .schema CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL); CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version"); CREATE TABLE "projects" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "created_at" datetime, "updated_at" datetime); sqlite> select * from projects; sqlite> .exit
projects tableにid,title,create_at,update_atとかいろいろあるけどまだなんも無いと。
インタラクティブにモデルをいじる
$ rails console Loading development environment (Rails 4.1.5) 2.1.1 :001 > p = Project.new(title: "p1") => #<Project id: nil, title: "p1", created_at: nil, updated_at: nil> 2.1.1 :002 > p.save (0.2ms) begin transaction SQL (0.3ms) INSERT INTO "projects" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", "2014-09-14 07:07:07.673895"], ["title", "p1"], ["updated_at", "2014-09-14 07:07:07.673895"]] (11.5ms) commit transaction => true
rails console
でRubyのコンソール立ち上げてp = Project.new(title: "p1")
でオブジェクト作って、p.save
すると。いろいろ作られるのでp
で確認
2.1.1 :003 > p => #<Project id: 1, title: "p1", created_at: "2014-09-14 07:07:07", updated_at: "2014-09-14 07:07:07">
Project.create()
でもよし。new
とsave
を同時に行ってくれる。
もう一つタイトルがp2
のプロジェクトを作る。
2.1.1 :004 > Project.create(title: "p2") (0.1ms) begin transaction SQL (0.3ms) INSERT INTO "projects" ("created_at", "title", "updated_at") VALUES (?, ?, ?) [["created_at", "2014-09-14 07:07:58.970630"], ["title", "p2"], ["updated_at", "2014-09-14 07:07:58.970630"]] (24.4ms) commit transaction => #<Project id: 2, title: "p2", created_at: "2014-09-14 07:07:58", updated_at: "2014-09-14 07:07:58">
Project.all
で確認してみる。
2.1.1 :005 > Project.all Project Load (0.2ms) SELECT "projects".* FROM "projects" => #<ActiveRecord::Relation [#<Project id: 1, title: "p1", created_at: "2014-09-14 07:07:07", updated_at: "2014-09-14 07:07:07">, #<Project id: 2, title: "p2", created_at: "2014-09-14 07:07:58", updated_at: "2014-09-14 07:07:58">]>
プロジェクトが2つできている。
2.1.1 :006 > quit
抜けた上でDBの確認
$ rails db SQLite version 3.8.2 2013-12-06 14:53:30 Enter ".help" for instructions Enter SQL statements terminated with a ";" sqlite> select * from projects; 1|p1|2014-09-14 07:07:07.673895|2014-09-14 07:07:07.673895 2|p2|2014-09-14 07:07:58.970630|2014-09-14 07:07:58.970630 sqlite> .exit