Skip to content

Instantly share code, notes, and snippets.

@heratyian
Created September 4, 2025 22:15
Show Gist options
  • Select an option

  • Save heratyian/5d2b9211f1418852db41420b1fd66303 to your computer and use it in GitHub Desktop.

Select an option

Save heratyian/5d2b9211f1418852db41420b1fd66303 to your computer and use it in GitHub Desktop.

Tech Meeting: 2025-09-04

Generators

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

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.

Example

  • 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).

How to add indexes in Rails

You define indexes in migrations:

class AddIndexToUsersEmail < ActiveRecord::Migration[7.0]
  def change
    add_index :users, :email, unique: true
  end
end
  • :users table name
  • :email column
  • unique: true ensures values in that column are unique (also enforces validation at DB level)

multi-column indexes (composite indexes)

add_index :orders, [:user_id, :created_at]

Why?

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_id index, then sort all rows by created_at, which is slower.

Best practices

  • 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.

Deep Dive

O(n) vs O(log(n)) (balanced tree)

Browser, Server, and Database: How They Interact

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

Loading

REST (Representational State Transfer)

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 data
  • POST: create something new
  • PUT/PATCH: update something
  • DELETE: remove something

So REST just says: use the web's existing rules (URLs + HTTP methods) to interact with data in a simple, consistent way.

RESTful

A service is called RESTful if it actually follows the REST principles.

That usually means:

  • Each resource has a unique URL (e.g., /users/123 for 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).

Example

  • GET /books/5 → get the book with ID 5.
  • POST /books → create a new book.
  • DELETE /books/5 → delete the book with ID 5.

Alternatives to REST

  • SOAP
  • GraphQL
  • gRPC
  • and more...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment