rails generate scaffold chirp user:references body:text deleted_at:datetime
resource vs controller vs model vs scaffold
using -h // --help flag in cli
indexes are database-level structures that make querying faster.
They live in the underlying database (PostgreSQL, MySQL, SQLite, etc.).
An index is a lookup table that helps the database find rows quickly without scanning the entire table.
- Without an index: searching for a User by email means the DB checks every row one by one.
- With an index on email: the DB jumps directly to the matching row(s).
Indexes speed up SELECT, WHERE, ORDER BY, and JOIN operations, but they make INSERT, UPDATE, and DELETE a bit slower (because the index itself must also be updated).
You define indexes in migrations:
class AddIndexToUsersEmail < ActiveRecord::Migration[7.0]
def change
add_index :users, :email, unique: true
end
end:userstable name:emailcolumnunique: trueensures values in that column are unique (also enforces validation at DB level)
add_index :orders, [:user_id, :created_at]
You often query like:
Order.where(user_id: 42).order(created_at: :desc)- The index helps the DB jump straight to all orders for that user and return them in date order efficiently.
- Without the index, Postgres might use the
user_idindex, then sort all rows bycreated_at, which is slower.
- Always index columns you frequently search, join, or filter on.
- Index foreign keys (user_id, post_id) for faster joins.
- Use unique indexes for things like email or username to enforce data integrity.
- Avoid indexing everything. each index adds storage and slows down writes.
O(n) vs O(log(n)) (balanced tree)
flowchart LR
subgraph Client
A[Browser]
end
subgraph Server
B[Web Server]
end
subgraph Database
C[(PostgreSQL)]
end
A -->|HTTP request| B
B -->|SQL query| C
C -->|query result| B
B -->|HTTP response| A
An architectural style for designing web services. The idea is, everything you interact with on the web is a resource (like a user, a blog post, or a product).
You access and manipulate these resources through standard HTTP methods:
GET: retrieve dataPOST: create something newPUT/PATCH: update somethingDELETE: remove something
So REST just says: use the web's existing rules (URLs + HTTP methods) to interact with data in a simple, consistent way.
A service is called RESTful if it actually follows the REST principles.
That usually means:
- Each resource has a unique URL (e.g.,
/users/123for user with ID 123). - The HTTP method determines the action (GET = read, POST = create, etc.).
- Responses use standard formats (like JSON) and return the right status codes (200 OK, 404 Not Found, 201 Created).
GET /books/5→ get the book with ID 5.POST /books→ create a new book.DELETE /books/5→ delete the book with ID 5.
- SOAP
- GraphQL
- gRPC
- and more...