ใน Rails Query Database ผ่าน Models เพื่อเข้าถึง Data — ใช้ ActiveRecord Methods
เช่น where, find, find_by
ผลลัพธ์:
find_by: Single Record หรือnilwhere:ActiveRecord::RelationObjectfind: Single Record by Primary Key (ปกติid) Raise Exception ถ้าไม่เจอ
พูดง่ายๆ: คาดหวัง 1 Record (User เฉพาะ) ใช้ find_by — หลาย Records (List of Users) ใช้ where
แต่ where มีหลายรูปแบบ ่ทำให้มือใหม่สับสน — ไม่เป็นไร มาดูทีละแบบ!
Basic Where Conditions
จุดประสงค์ where คือ Build Query Filter Database ให้ได้ Rows ที่ต้องการ
ตัวอย่าง: Model Book มี title, author, category — อยากหาหนังสือโดย Author เฉพาะ หรือ Category เฉพาะ
Query:
Book.where(category: "Ruby")
คืนหนังสือ Category "Ruby" — รวมเงื่อนไขได้:
Book.where(category: "Ruby", author: "Jesus Castello")
Match ทั้ง Category และ Author — รวม where กับ Scope ได้:
# scope definition
class Book
scope :long_title, -> { where("LENGTH(title) > 20") }
end
# controller code
@books = Book.long_title.where(category: "Ruby")
Hash (fruit: "apple") Check Column เท่ากับค่า — แต่ถ้าไม่ใช่ Equality?
ต้องใช้ String
Rails Where: Greater Than & Less Than
อยาก Check "Greater Than", "Less Than" — ทำแบบนี้:
Book.where("LENGTH(title) > 20")
Interpolate Values — ทำแบบนี้:
Book.where("LENGTH(title) > ?", params[:min_length])
? = "Placeholder" ใช้เพื่อ Security ป้องกัน "SQL Injection"
อีกทางคือ Named Placeholders:
Book.where("LENGTH(title) > :min", min: params[:min_length])
น้อยกว่า ? Placeholder แต่อาจช่วย Query ที่ Interpolate Values เยอะ
วิธีใช้ Where Not & Where Or Conditions
ดู Complex Queries — อยาก Check Condition NOT True ใช้ where กับ not
ตัวอย่าง:
Book.where.not(category: "Java")
อีกตัว:
Book.where.not(title: nil)
หา Books ที่ Title ไม่ใช่ nil — OR Condition? รวม where กับ or ได้ Rows ที่ Match ANY Condition
ทำงานแบบนี้:
Book.where(category: "Programming").or(Book.where(category: "Ruby"))
รวม 2 Where Queries เป็น 1 — ลองดูสิ!
Rails Where IN Array Example
ตัวอย่างก่อนหน้า找 1 ค่า — แต找 Multiple Values ได้ด้วย Array!
ทำยังไง?
Book.where(id: [1,2,3])
Generate "IN" Query ค้นหา IDs พร้อมกัน — ใน Rails Logs จะเห็น:
SELECT "books".* FROM "books" WHERE "books"."id" IN (1, 2, 3)
Rails Joins Association Condition
Match ตาม Association Value — ต้องทำ Association Available ด้วย joins
แล้วทำได้:
Book.joins(:comments).where(comments: { id: 2 })
ได้ Books ที่มี Comment และ Comment ID = 2 — String Version:
Book.joins(:comments).where("comments.id = 2")
comments = Table Name, id = Column Name
ค้นหาในข้อความด้วย Where LIKE Condition
ทำ Search Function อยาก Partial Match — ใช้ "LIKE" Query
ตัวอย่าง:
Book.where("title LIKE ?", "%" + params[:q] + "%")
หา Titles ที่มี Search Text (จาก params[:q]) อยู่ไหนใน Title ก็ได้ — Query มีประโยชน์มาก!
Percent Symbol คืออะไร? ใน SQL % = Wildcard Character
Video: Where vs Find_By in Detail
Summary (สรุป)
คุณเรียนรู้ใช้ Rails where Query ActiveRecord Models!
รวมถึง:
- Filter ด้วย Multiple Conditions
- ใช้ Arrays หา Multiple Rows ได้มีประสิทธิภาพ
- รวม
whereกับorและnotเขียน Complex Queries
ตอนนี้ถึงคุณ Practice — ขอบคุณที่อ่าน 🙂