2020. 6. 11. 11:16ㆍRails 5 on aws c9
이번엔 레일즈 콘솔에 대해 알아보고, 디비의 내용을 저장하고 불러오는 방법을 알아보겠다.
rails c를 입력하면 콘솔이 나온다.
2.6.3 :007 > puts "hello world!"
hello world!
=> nil
레일즈 환경이 모두 로드 되어있기 때문에 이전 강의에서 만들었던 Contect 모델도 불러올 수 있다.
Contact.all 은 Contact 모델에 연결되어있는 contects table에 있는 모든 내용을 불러오는 명령어다.
2.6.3 :008 > Contact.all
Contact Load (7.0ms) SELECT "contacts".* FROM "contacts"
=> #<ActiveRecord::Relation []>
대괄호가 비어있는걸 볼 수 있다(아직 아무 값도 안넣어서) 루비 배열에서 비어있는거와 같다 할 수 있다.
그러면 이 contacts table에 첫 데이터를 넣어보겠다.
2.6.3 :009 > first_contact = Contact.new
=> #<Contact id: nil, email: nil, content: nil, created_at: nil, updated_at: nil, name: nil>
여기서 각각의 내용을 채우는 방법은
2.6.3 :012 > first_contact.email = "first@naver.com"
=> "first@naver.com"
2.6.3 :013 > first_contact
=> #<Contact id: nil, ***email: "first@naver.com"***, content: nil, created_at: nil, updated_at: nil, name: nil>
나머지 name과 content도 넣어보자.
2.6.3 :014 > first_contact.name = "uesr_f"
=> "uesr_f"
2.6.3 :015 > first_contact.content = "hello world!"
=> "hello world!"
2.6.3 :016 > first_contact
=> #<Contact id: nil, email: "first@naver.com", content: "hello world!", created_at: nil, updated_at: nil, name: "uesr_f">
id, create_at, update_at 은 비어있다. 이 값은 데이터베이스에서 save가 일어날 때 순서대로 id가 할당되고, 시간으로 값이 저장되니까 직접 입력하는건 아니다.
여기까지했으면 지금당작 contact 데이터베이스를 전부 불러오면 여전히 비어있는걸 볼 수 있다.
왜냐하면 아직까지는 만든 객체가 메모리상에만 있기 때문이다.
이를 데이터베이스에 저장을 하려면 (변수).save 라는 명령어를 실행시키면 된다.
2.6.3 :017 > Contact.all
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts"
***=> #<ActiveRecord::Relation []>***
***2.6.3 :018 > first_contact.save***
(0.1ms) begin transaction
SQL (1.7ms) INSERT INTO "contacts" ("email", "content", "created_at", "updated_at", "name") VALUES (?, ?, ?, ?, ?) [["email", "first@naver.com"], ["content", "hello world!"], ["created_at", "2020-06-09 09:55:13.480098"], ["updated_at", "2020-06-09 09:55:13.480098"], ["name", "uesr_f"]]
(4.8ms) commit transaction
=> true
INSERT INTO "contacts" ("email", "content", "created_at", "updated_at", "name") VALUES (?, ?, ?, ?, ?)
이게 sql문이다. .save로 자동으로 레일즈가 작성해주니, 몰라도 그냥 쓰면된다.
2.6.3 :019 > Contact.all
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts"
=> #<ActiveRecord::Relation [#<Contact id: 1, email: "first@naver.com", content: "hello world!", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 09:55:13", name: "uesr_f">]>
이제는 Contact.all 하면 []가 아니라 값이 나오는걸 볼 수 있다.
이를 한번에 저장하는 방법도 알아보겠다.
2.6.3 :021 > Contact.create(name: 'john', email: 'ghon@gmail.com', content: 'good afternoon')
(0.1ms) begin transaction
SQL (2.0ms) INSERT INTO "contacts" ("email", "content", "created_at", "updated_at", "name") VALUES (?, ?, ?, ?, ?) [["email", "ghon@gmail.com"], ["content", "good afternoon"], ["created_at", "2020-06-09 09:58:44.934115"], ["updated_at", "2020-06-09 09:58:44.934115"], ["name", "john"]]
(9.1ms) commit transaction
=> #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">
이렇게하면 한번에 모든처리가 된 걸 볼 수 있다.
2.6.3 :022 > Contact.all
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts"
=> #<ActiveRecord::Relation [#<Contact id: 1, email: "first@naver.com", content: "hello world!", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 09:55:13", name: "uesr_f">, #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">]>
이젠 데이터베이스에서 값을 찾아오는 방법을 알아보겠다.
find(숫자) 를 하면 id를 기반으로 찾아온다, find_by([변수]:[값]) 하면 원하는 변수의 값으로 가져온다.
2.6.3 :023 > a = Contact.find(2)
Contact Load (0.2ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? LIMIT ? [["id", 2], ["LIMIT", 1]]
=> #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">
2.6.3 :024 > a = Contact.find_by(id:1)
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> #<Contact id: 1, email: "first@naver.com", content: "hello world!", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 09:55:13", name: "uesr_f">
a에 1번이 잘 로드된걸 확인할 수 있다.
2.6.3 :025 > a.name
=> "uesr_f"
2.6.3 :026 > a.email
=> "first@naver.com"
이번엔 where문을 사용해보겠다. where문을 이용하면 값으로 찾을 수 있다.
2.6.3 :030 > c = Contact.where(name: 'john')
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts" WHERE "contacts"."name" = ? [["name", "john"]]
=> #<ActiveRecord::Relation [#<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">]>
값을 배열로 받아온것을 볼 수 있다. 여러개일수도 있으니까.
그 외에도 Contact.first 하면 처음 저장된걸, Contact.last하면 마지막에 저장된걸 불러올 수 있다.
2.6.3 :031 > Contact.first
Contact Load (0.2ms) SELECT "contacts".* FROM "contacts" ORDER BY "contacts"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<Contact id: 1, email: "first@naver.com", content: "hello world!", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 09:55:13", name: "uesr_f">
2.6.3 :032 > Contact.last
Contact Load (2.1ms) SELECT "contacts".* FROM "contacts" ORDER BY "contacts"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">
그럼 a의 content 값을 수정해보겠다.
2.6.3 :033 > a
=> #<Contact id: 1, email: "first@naver.com", content: "hello world!", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 09:55:13", name: "uesr_f">
2.6.3 :034 > a.content = "changed content"
=> "changed content"
2.6.3 :035 > a
=> #<Contact id: 1, email: "first@naver.com", content: "changed content", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 09:55:13", name: "uesr_f">
하지만 이건 메모리에서만 변화가 일어난것이다.
이를 디비에 적용하려면 a.save를 하면 된다.
2.6.3 :036 > Contact.all
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts"
=> #<ActiveRecord::Relation [#<Contact id: 1, email: "first@naver.com", content: "hello world!", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 09:55:13", name: "uesr_f">, #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">]>
2.6.3 :037 > a.save
(0.1ms) begin transaction
SQL (2.6ms) UPDATE "contacts" SET "content" = ?, "updated_at" = ? WHERE "contacts"."id" = ? [["content", "changed content"], ["updated_at", "2020-06-09 10:09:27.679582"], ["id", 1]]
(7.1ms) commit transaction
=> true
2.6.3 :038 > Contact.all
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts"
=> #<ActiveRecord::Relation [#<Contact id: 1, email: "first@naver.com", content: "changed content", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 10:09:27", name: "uesr_f">, #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">]>
이번엔 삭제를 해보겠다.
d에 마지막 값을 불러오고 destroy를 호출해 삭제한다. 값이 사라진걸 확인할 수 있다.
2.6.3 :040 > d = Contact.last
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts" ORDER BY "contacts"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">
2.6.3 :041 > d.destroy
(0.1ms) begin transaction
SQL (1.4ms) DELETE FROM "contacts" WHERE "contacts"."id" = ? [["id", 2]]
(10.4ms) commit transaction
=> #<Contact id: 2, email: "ghon@gmail.com", content: "good afternoon", created_at: "2020-06-09 09:58:44", updated_at: "2020-06-09 09:58:44", name: "john">
2.6.3 :043 > Contact.all
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts"
=> #<ActiveRecord::Relation [#<Contact id: 1, email: "first@naver.com", content: "changed content", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 10:09:27", name: "uesr_f">]>
Contact.create로 새로운 값을 입력해보겠다.
Alice가 3번에 추가된걸 볼 수 있다
2.6.3 :047 > Contact.create(name: 'Alice', email: 'alice@naver.com', content: 'I am alice')
(0.1ms) begin transaction
SQL (2.5ms) INSERT INTO "contacts" ("email", "content", "created_at", "updated_at", "name") VALUES (?, ?, ?, ?, ?) [["email", "alice@naver.com"], ["content", "I am alice"], ["created_at", "2020-06-09 10:14:47.457118"], ["updated_at", "2020-06-09 10:14:47.457118"], ["name", "Alice"]]
(10.2ms) commit transaction
=> #<Contact id: 3, email: "alice@naver.com", content: "I am alice", created_at: "2020-06-09 10:14:47", updated_at: "2020-06-09 10:14:47", name: "Alice">
2.6.3 :048 > Contact.all
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts"
=> #<ActiveRecord::Relation [#<Contact id: 1, email: "first@naver.com", content: "changed content", created_at: "2020-06-09 09:55:13", updated_at: "2020-06-09 10:09:27", name: "uesr_f">, #<Contact id: 3, email: "alice@naver.com", content: "I am alice", created_at: "2020-06-09 10:14:47", updated_at: "2020-06-09 10:14:47", name: "Alice">]>
마지막으로 업데이트를 한번에 하는법을 알아보겠다.
(변수).update 함수를 호출하면 된다.
2.6.3 :049 > m = Contact.last
Contact Load (0.1ms) SELECT "contacts".* FROM "contacts" ORDER BY "contacts"."id" DESC LIMIT ? [["LIMIT", 1]]
=> #<Contact id: 3, email: "alice@naver.com", content: "I am alice", created_at: "2020-06-09 10:14:47", updated_at: "2020-06-09 10:14:47", name: "Alice">
2.6.3 :050 > m.update(content: "hello world")
(0.1ms) begin transaction
SQL (1.8ms) UPDATE "contacts" SET "content" = ?, "updated_at" = ? WHERE "contacts"."id" = ? [["content", "hello world"], ["updated_at", "2020-06-09 10:17:08.716870"], ["id", 3]]
(5.4ms) commit transaction
=> true
'Rails 5 on aws c9' 카테고리의 다른 글
13. route에 대한 이해 (0) | 2020.06.11 |
---|---|
12. Contact 작성 기능 구현 (0) | 2020.06.11 |
10. Model과 데이터베이스 (0) | 2020.06.11 |
9. Form 헬퍼 (0) | 2020.06.11 |
8. Partial을 이용해 효율적으로 코드 관리하기 (0) | 2020.06.11 |