LoginSignup
0
2

More than 5 years have passed since last update.

[Progate] Rails自分用

Posted at

マイグレーションファイルを作成した場合は必ず$ rails db:migrateを実行する必要があります。

モデル, マイグレーションファイルの作成

$ rails g model Post content:text

  • Post: モデル名
  • content: カラム名
  • text: データ型

コンソールを起動

ターミナル上で$ rails console> quitでコンソール終了。

データベースの posts テーブルに、データを追加

  1. new メソッドで Post モデルのインスタンスを作成
  2. posts テーブルに保存

posts テーブルに保存されているデータを取得

posts テーブルの最初のデータを取得

$ rails console
> post = Post.first
> post.content # Post.firstで取得したデータから投稿内容を取得
$ rails console
> post = Post.all # すべてのデータを取得
> post = Post.all[0] # インデックス番号で要素を指定して取り出す
> Post.all[0].content # 投稿内容を取得
0
2
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
0
2