Skip to content

Instantly share code, notes, and snippets.

View bensheldon's full-sized avatar
🏊‍♂️
Swimming through code.

Ben Sheldon [he/him] bensheldon

🏊‍♂️
Swimming through code.
View GitHub Profile

In my application every time I send a newsletter to a User, I create a NewsletterDelivery record. I frequently want to be able to query, for each user, what is the most recent newsletter delivery record. This is called a "last n per group" query and a LATERAL JOIN is the best way to do it imo. But the query I've been using (and I've told people to use, and seen blogged about) is not very good, because the conditions don't get pushed down into the subselect which means that the query ends-up lateral-joining all the records before it applies the conditions for the association.

Instead of doing subselect_table.* the better query does association.id AS assocation_id, subselect_table.id, subselect_table.title, .... and enumerates over all of the columns. This allows the association query, which Active Record tacks on at the end as WHERE association_id = $1 or WHERE association_id IN ($1, $2, $3, ...) to be pushed down c

#!/usr/bin/env ruby
# Place in your project (don't forget to chmod +x):
# bin/autoload-check
# Allow these files to be fixed later
allowlist = [
"ExampleClass" # why?
]

Investigating memory growth with Sheap in a Rails application.

Ran this locally because of an issue with rbtrace not working with Puma when requests aren't actively arriving:

while true; do
  status_code=$(curl -o /dev/null -s -w "%{http_code}\n" -I "https://dayoftheshirt.com")
  echo "Status code: $status_code"
  sleep 1
done
# bin/rails-production
#!/usr/bin/env ruby
# Simulate a production environment locally
# for the purpose of profiling
ENV['RAILS_ENV'] = 'production'
ENV['NODE_ENV'] = 'production'
ENV['RACK_ENV'] = 'production'
diff --git a/app/controllers/account/registrations_controller.rb b/app/controllers/account/registrations_controller.rb
index 376f20a77..a554c9f8d 100644
--- a/app/controllers/account/registrations_controller.rb
+++ b/app/controllers/account/registrations_controller.rb
@@ -1,6 +1,8 @@
# frozen_string_literal: true
module Account
class RegistrationsController < Devise::RegistrationsController
+ before_action :reject_spam, only: :create
+
@bensheldon
bensheldon / add-geekbot-questions.js
Last active July 11, 2023 18:40 — forked from JoseInTheArena/add-geekbot-questions.js
Script for adding questions to a geekbot random question type
/**
* Disclaimer: This works as of the date of writing (7/11/2023) but there's no guarantees it will work in the future
* as it depends on selectors on geekbot's web UI, which may change at any time. Use at your own risk.
*
* To use this:
* 1. Navigate to your geekbot random question settings and click "Edit questions"
* so that you see a list of input fields and an "+ Add question" button
* 2. Open your devtools
* 3. Pasting this script into the console
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~>6.1'
end

Emails and SMS. Cousins or Sisters?

Many apps send both emails and SMS messages via Twilio.

My experience with GCF

Emails go out through Rails built-in Action Mailer construct

Raise your hand. Who here has worked with Rails Action Mailer?

ActionMailer:

@bensheldon
bensheldon / credential.rb
Created January 12, 2019 00:20
Example of using Postgres advisory locks with Rails ActiveRecord
class Credential < ApplicationRecord
CredentialNotAvailableError = Class.new(StandardError)
def self.find_and_lock_credential
where("pg_try_advisory_lock(to_regclass('#{table_name}')::int, id::int)").first
end
def self.find_and_lock_credential!
find_and_lock_credential || raise(CredentialNotAvailableError)
end