ruby

ruby
class RemindersController < ApplicationController
  def create
    zone = ActiveSupport::TimeZone[current_member.time_zone]
    local_time = zone.parse(params.require(:reminder).fetch(:scheduled_for))

    Reminder.create!(member: current_member, scheduled_for: local_time.utc)

Time Zone Safe Scheduling

rails timezones correctness
by Sarah Chen 1 tab
ruby
class PostsController < ApplicationController
  def create
    @post = current_member.posts.build(post_params)

    if @post.save
      flash.now[:notice] = 'Post created.'

Turbo Stream flash messages without custom JS

rails hotwire turbo
by Henry Kim 2 tabs
ruby
module Api
  module V1
    class PostsController < BaseController
      before_action :authenticate_user!

      def create

Strong parameters for mass assignment protection

rails security api
by Alex Kumar 1 tab
ruby
class DependencyError < StandardError
  attr_reader :context

  def initialize(message, context = {})
    @context = context
    super(message)

Structured Exceptions with Context

rails reliability observability
by Sarah Chen 2 tabs
ruby
class Order < ApplicationRecord
  has_many :line_items, strict_loading: true
  belongs_to :customer, strict_loading: true

  def total_cents
    line_items.sum { |li| li.quantity * li.unit_price_cents }

Strict Loading to Catch N+1 in Development

rails activerecord performance
by Sarah Chen 2 tabs
ruby
class Notification < ApplicationRecord
  belongs_to :member

  after_create_commit -> { broadcast_prepend_to stream_name, target: "notifications", partial: "notifications/notification", locals: { notification: self } }
  after_update_commit -> { broadcast_replace_to stream_name, partial: "notifications/notification", locals: { notification: self } }

Model broadcasts: prepend on create, replace on update

rails turbo hotwire
by Sarah Chen 2 tabs
yaml
production:
  adapter: postgresql
  encoding: unicode
  pool: <%= ENV.fetch("RAILS_MAX_THREADS", 5) %>
  timeout: 5000
  checkout_timeout: 5

Database connection pooling configuration

rails database performance
by Alex Kumar 2 tabs
ruby
class ProjectsController < ApplicationController
  def create
    @project = Project.new(project_params)

    if @project.save
      response.set_header('Turbo-Location', project_url(@project))

Turbo-Location header: redirect a frame submission to a new URL

rails hotwire turbo
by Henry Kim 3 tabs
ruby
class AddMetadataToUsers < ActiveRecord::Migration[6.1]
  def change
    add_column :users, :metadata, :jsonb, default: {}, null: false
    add_index :users, :metadata, using: :gin
  end
end

JSON column for flexible schema extensions

rails postgresql database
by Alex Kumar 3 tabs
ruby
class CircuitBreaker
  def initialize(name, redis: Redis.current)
    @name = name
    @redis = redis
  end

Service-Level “Circuit Breaker” (Simple)

rails reliability http
by Sarah Chen 2 tabs