2
3

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.

モデルの基本

Posted at

モデルはデータベースや外部サービスへのアクセス等
“データ”の管理、操作を担当するコンポーネントのこと。

Railsには標準でO/Rマッパー
Active Recordがついている。
(初学者はActive Recordをオススメ)

O/R mapperとは:
リレーショナルデータベースとオブジェクト指向言語の
橋渡しを受け持つライブラリの事です。

リレーショナルモデル=データベース
オブジェクトモデル=アプリケーション
この2つは全然違う

データベース接続の設定:
Active Record経由でデータベースに接続するには、
config/databse.ymlにて接続設定を定義する必要がある。

デフォルトの設定:
development : 開発環境用
.
.
.
test : テスト環境用
.
.
.
production : 本番環境用
.
.
.

database.ymlは(ヤムル)で記述
YAML = YAML ain't markup languageの略

マークアップ言語ではなく、構造をインデントや記号で表現する。

複雑な構造を表現するには不向き
シンプルさはすごい

YAMLの基本パラメータ表示形式:
production
adapter:splite3

インデントはタブでなくスペース2つ!

目的に応じて環境を使い分ける
そうすることで、開発環境での操作が不用意に
本番に影響する事を妨げる。

モデルクラスの作成
データベースのテーブルにアクセスするための
モデルクラスを作成。

ここでもrails generate コマンドを利用

rails generate model name field:type [...] [options]

テーブルを作成:

isbn string ISBN code
title " 書名
price integer 価格
publish string 出版社
published date 刊行日
cd boolean CD・添付

モデル関連の命名規則→命名規則が重要!!

model class=Capital, Singular : Book
model class(file)=small, singular : book.rb
table=small, plural : books
test script= small_test.rb, singular : book_test.rb

2
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?