Git Product home page Git Product logo

myfirstruby's Introduction

README

This README would normally document whatever steps are necessary to get the application up and running.

Things you may want to cover:

  • Ruby Create project
rails new project2 --api --database=sqlite3 --skip-bundle

สร้าง Gemset

echo 'ror-project2' > .ruby-gemset

ออกจาก folder project2 และกลับเข้าไปใหม่อีกที

cd..
cd project2
  • CLI

Create model

rails generate model User first_name:text last_name:text

Run migrate

rails db:migrate

Rollback migrate

rails db:rollback 

ถ้าจะ rollback 5 ครั้วให้ทำ STEP=5 ต่อท้าย

rails db:rollback STEP=5

Rollback เวอชันเดียว

rails db:migrate:down VERSION=YYYYMMDDHHmmss

Create migration file example หากเราขึ้นต้นด้วย Add ต่อด้วย To ลงท้ายด้วยชื่อ Model ที่ถูกต้อง Rails จะเดาว่าเราต้องการแก้ไข Model ไหน โดยที่เราไม่ต้องไปพิมพ์ 3 บรรทัดเอง หากจะลบ Column ให้ขึ้นต้นด้วย Remove ต่อด้วยคำว่า From ลงท้ายด้วย Model

rails generate migration AddDobToUser date_of_birth:date
rails g migration RemoveTitleFromUser title:text

Open rails console at root

rails console
rails c

Create user from rails console เป็นการสร้าง Object จาก User Class หลังจากสร้าง จะได้ Object กลับมาและมี Column ตาม Table ที่สร้างไว้

u = User.new

Commit object for save to database

u.save

Query user ออกมาดู 1 คน

User.find(:id)
User.find(1) 
u = User.find(1)

Add data example

u.first_name = 'Supachai'

Delete data example

u.destroy

หลังจาก Destroy จะยังใช้ u ได้อยู่เพราะ u ยังอยู่ใน memmory

ความแตกต่าง .save and .create User.create() จะ return User Object ที่ถูกสร้างกลับมา โดยที่เราใส่ไว้ใน u1, u2 ได้ซึ่งเทียบ เท่ากับการรัน User.new() และนำไป save

เราใช้ u3 ทำ User.new() ไว้ มันจะ return User Object ออกมา แต่ถ้าเรา u3.save มันจะเป็น boolean บอกแค่ว่า save สำเร็จหรือไม่

๊User.find_by เป็น method สำหรับทุก Column ใน table ชื่อ user เช่น first_name. last_name ตัวนี้จะ limit 1 ตัวแปรที่ใช้ find_by สามารถ Destroy ได้

Get all example แบบนี้เราจะได้เป็น Object ออกมา เราต้องเอาไป .map ออกมา

User.all

???

User.reload

Method ที่น่าสนใจ

ถามว่า User object นี้เคยบันทึกลง Database หรือยัง

user.new_record?

ถาม User ว่า User object นี้เคยถูกเปลี่ยนแปลงค่าหรือไม่ หลังจากที่ save หรือ load จาก database หากลอง save แล้วทดสอบด้วย method นี้อีกครั้งจะได้เป็น false

user.changed?

Query หาข้อมูลที่ต้องการจาก Database โดยที่ข้อมูลที่ได้มาจะเป็น Array สิ่งที่หาจะเป็น Case sensitive เท่านั้น ต่อให้ดึงค่าออกมาแล้วไม่เจอก็จะยังได้อยู่ดีแต่ว่าเป็น .count เท่ากับ 0 ฉะนั้นถ้าอยากเอาค่าข้างในออกมาใช้งานจะต้องใช้ users[index] ออกมาใช้งาน

User.where()

ทุกๆ Active Record Object นั้นเป็น Serializable Object ทำให้เราสามารถแปลงค่าเป็น json ได้ ทันทีด้วย .as_json ซึ่งจะออกมาเป็น Ruby Hash แต่ถ้าอยากได้ JSON String ให้ใช้ .to_json

users.as_json
  • System dependencies

  • Configuration

  • Database creation

  • Database initialization

  • How to run the test suite

  • Services (job queues, cache servers, search engines, etc.)

  • Deployment instructions

  • ... //

myfirstruby's People

Contributors

bankbeyond avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.