일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- Python
- vscode
- 디지몬
- 일본어
- 자바
- メソッド
- Flutter
- C로 시작하는 컴퓨터 프로그래밍4판
- DART
- jsp
- rails
- 単語
- java
- 日本語
- 一日一つメソッド
- Spring
- CSS
- springboot
- ruby
- nico
- 건담
- 인프런
- 연습문제
- html
- 반다이몰
- Web
- 비즈니스일본어
- javascript
- 건담베이스
- rails7
Archives
- Today
- Total
AR삽질러
初めてのRuby on Rails データベースの確認、操作、表紙(4) 본문
728x90
初めてのRuby on Rails データベースの確認、操作、表紙(4)
1. データベースの確認
DatabaseをCommandで操作できるモード
rails dbconsole
テーブルリスト確認
- コマンドを使用してデータベース内のすべてのテーブルを確認る。
.tables [テーブル名]
sqlite> .schema [テーブル名]
コマンドを使用して、特定のテーブルのスキーマを確認。
.schema users
CREATE TABLE IF NOT EXISTS "users" (
"id" integer PRIMARY KEY AUTOINCREMENT NOT NULL,
"name" varchar,
"age" integer,
"created_at" datetime(6) NOT NULL,
"updated_at" datetime(6) NOT NULL);
SQLクエリを使用してデータを確認できる。
select * from users;
終了 : .quit. / .q / .exit
2. データベースの操作
rails Consoleを開く
rails c / rails console
データを確認する
User.all
irb(main):001> User.all
User Load (0.1ms) SELECT "users".* FROM "users"
=> []
データを作る
User.new
#newメソッドは新しいインスタンスをメモリにのみ生成する。 これはデータベースには反映されない。
#生成されたインスタンスは、後で必要なプロパティを設定した後、saveメソッドを呼び出してデータベースに保存できる。
#newは、データを保存する前にインスタンスの追加作業や検証が必要なときに便利。
User.create
#createメソッドはモデルの新しいインスタンスを生成し、そのインスタンスをデータベースにすぐに保存する。
#createはnewとsaveを合わせたものと同じ作業を1段階で行える。
#インスタンスの作成と同時に保存が必要なときに便利。
rb(main):002> user = User.new
=> #<User:0x0000000109ef1f78 id: nil, name: nil, age: nil, created_at: nil, updated_at: nil>
irb(main):003> user.name = 'Arang'
=> "Arang"
irb(main):004> user.age = 20
=> 20
newで作れられたインスタンスを保存
user.save
irb(main):005> user.save
TRANSACTION (0.1ms) begin transaction
User Create (1.4ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "Arang"], ["age", 20], ["created_at", "2023-12-31 11:00:05.589577"], ["updated_at", "2023-12-31 11:00:05.589577"]]
TRANSACTION (1.6ms) commit transaction
=> true
Userを確認する
irb(main):010> User.all
User Load (0.3ms) SELECT "users".* FROM "users"
=>
[#<User:0x0000000109b3dd50
id: 1,
name: "Arang",
age: 20,
created_at: Sun, 31 Dec 2023 11:00:05.589577000 UTC +00:00,
updated_at: Sun, 31 Dec 2023 11:00:05.589577000 UTC +00:00>,
#<User:0x0000000109b3dc10
データをUpdateする
user.update
irb(main):015> user
=>
#<User:0x000000010a974ec8
id: 1,
name: "Arang",
age: 20,
created_at: Sun, 31 Dec 2023 11:00:05.589577000 UTC +00:00,
updated_at: Sun, 31 Dec 2023 11:00:05.589577000 UTC +00:00>
irb(main):016>
irb(main):017> user.update(age: 22)
TRANSACTION (0.1ms) begin transaction
User Update (1.0ms) UPDATE "users" SET "age" = ?, "updated_at" = ? WHERE "users"."id" = ? [["age", 22], ["updated_at", "2023-12-31 11:14:55.742065"], ["id", 1]]
TRANSACTION (1.9ms) commit transaction
=> true
irb(main):018> user
=>
#<User:0x000000010a974ec8
id: 1,
name: "Arang",
age: 22,
created_at: Sun, 31 Dec 2023 11:00:05.589577000 UTC +00:00,
updated_at: Sun, 31 Dec 2023 11:14:55.742065000 UTC +00:00>
データをDeleteする
user.destroy
irb(main):019> user = User.find(2)
User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
=>
#<User:0x000000010a97f288
...
irb(main):020> user
=>
#<User:0x000000010a97f288
id: 2,
name: "hana",
age: 25,
created_at: Sun, 31 Dec 2023 11:00:50.010306000 UTC +00:00,
updated_at: Sun, 31 Dec 2023 11:00:50.010306000 UTC +00:00>
irb(main):021> user.destroy
TRANSACTION (0.1ms) begin transaction
User Destroy (0.9ms) DELETE FROM "users" WHERE "users"."id" = ? [["id", 2]]
TRANSACTION (1.6ms) commit transaction
=>
#<User:0x000000010a97f288
id: 2,
name: "hana",
age: 25,
created_at: Sun, 31 Dec 2023 11:00:50.010306000 UTC +00:00,
updated_at: Sun, 31 Dec 2023 11:00:50.010306000 UTC +00:00>
irb(main):022> User.all
User Load (0.4ms) SELECT "users".* FROM "users"
=>
[#<User:0x000000010a97b7c8
id: 1,
name: "Arang",
age: 22,
created_at: Sun, 31 Dec 2023 11:00:05.589577000 UTC +00:00,
updated_at: Sun, 31 Dec 2023 11:14:55.742065000 UTC +00:00>]
Rails Console終了
exit / control + d
3. データベースの表紙
例題
登録するユーザーデータ
name : ogumon | age : 20 | name : metal greymon | age : 40 |
name : greymon | age : 30 | name : wargreymon | age : 50 |
irb(main):002> agumon = User.new(name: "agumon", age: 20)
=> #<User:0x0000000105f32db0 id: nil, name: "agumon", age: 20, created_at: nil, updated_at: nil>
irb(main):003> agumon.save
TRANSACTION (0.1ms) begin transaction
User Create (0.9ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "agumon"], ["age", 20], ["created_at", "2023-12-31 11:29:27.819163"], ["updated_at", "2023-12-31 11:29:27.819163"]]
TRANSACTION (1.4ms) commit transaction
=> true
irb(main):004> greymon = User.new(name: "greymon", age: 30)
=> #<User:0x0000000105eb85d8 id: nil, name: "greymon", age: 30, created_at: nil, updated_at: nil>
irb(main):005> greymon.save
TRANSACTION (0.1ms) begin transaction
User Create (1.0ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "greymon"], ["age", 30], ["created_at", "2023-12-31 11:29:52.551931"], ["updated_at", "2023-12-31 11:29:52.551931"]]
TRANSACTION (1.4ms) commit transaction
=> true
irb(main):006> metalgreymon = User.new(name: "metalgreymon", age: 40)
=> #<User:0x0000000107318848 id: nil, name: "metalgreymon", age: 40, created_at: nil, updated_at: nil>
irb(main):008> metalgreymon.save
TRANSACTION (0.1ms) begin transaction
User Create (0.8ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "metalgreymon"], ["age", 40], ["created_at", "2023-12-31 11:30:25.943705"], ["updated_at", "2023-12-31 11:30:25.943705"]]
TRANSACTION (1.4ms) commit transaction
=> true
irb(main):009> wargreymon = User.new(name: "wargreymon", age: 50)
=> #<User:0x00000001071ba280 id: nil, name: "wargreymon", age: 50, created_at: nil, updated_at: nil>
irb(main):011> wargreymon.save
TRANSACTION (0.1ms) begin transaction
User Create (1.1ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "wargreymon"], ["age", 50], ["created_at", "2023-12-31 11:31:01.477176"], ["updated_at", "2023-12-31 11:31:01.477176"]]
TRANSACTION (1.4ms) commit transaction
=> true
userscontroller.rb
irb(main):011> wargreymon.save
TRANSACTION (0.1ms) begin transaction
User Create (1.1ms) INSERT INTO "users" ("name", "age", "created_at", "updated_at") VALUES (?, ?, ?, ?) [["name", "wargreymon"], ["age", 50], ["created_at", "2023-12-31 11:31:01.477176"], ["updated_at", "2023-12-31 11:31:01.477176"]]
TRANSACTION (1.4ms) commit transaction
=> true
index.html.erb
<h1>get users Users#index</h1>
<p>Find me in app/views/users/index.html.erb</p>
<h1><%= @hello %><h1>
<p><%= @num %></p>
<ul>
<% @users.each do |user| %>
<li>
<%= user.id %>, <%= user.name %>, <%= user.age %>
</li>
<% end %>
</ul>
<% %> : Rubyコードを実行するが、結果を出力しない。 反復文、条件文、変数割り当てなどに使用。
<%= %> : Ruby コードを実行し、結果をHTML で出力する。 主に変数の値や計算結果をページに表示するときに使用。
https://github.com/designAR/First_Rails_introduction/tree/rails_console
728x90
반응형
LIST
'Ruby > RubyOnRails-日本語' 카테고리의 다른 글
初めてのRuby on Rails ミニQ&Aサイト開発 - 環境構築(1) (0) | 2023.12.31 |
---|---|
初めてのRuby on Rails ミニQ&Aサイト開発 (0) (0) | 2023.12.31 |
初めてのRuby on Rails ControllerからViewに、Model(3) (2) | 2023.12.31 |
Rails 一日一つメソッド (Day11) + form_with, link_to, each, present?メソッド (0) | 2023.12.31 |
Rails 一日一つメソッド (Day11) whereメソッド (0) | 2023.12.31 |