LoginSignup
0

More than 3 years have passed since last update.

[rails]modelを作成する方法

Last updated at Posted at 2020-07-30

railsでmodelを作成する方法

modelはデータベースとのやりとりを行ってくれます。
modelを作成するコマンドはこんな感じです。

$ rails g model モデル名

モデル名の命名規則は、「User」のように、英数字の単数形で指定します。
また、先頭は必ず英大文字で記述します。

実際に、modelを作成するためのコマンドを打つとこんな感じです。

$ rails g model User

成功すると、、、

$ rails g model User
Running via Spring preloader in process 4271
      invoke  active_record
      create    db/migrate/2020072951856_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/models/user_test.rb
      create      test/fixtures/users.yml

作成されるファイルは以下の4つです。

db/migrate/(作成日時)_create_lists.rb:「マイグレーションファイル」というデータベースの設計図になるファイルです。
app/models/list.rb:モデルクラスはデータベースに対応したRubyのクラスです。
test/models/list_test.rb:モデルクラスのテストコードのひな形です。
test/fixtures/lists.yml:テストデータ作成のためのひな形です。

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
0