Skip to content

Instantly share code, notes, and snippets.

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

@scart88
scart88 / script.rb
Created July 12, 2024 17:58 — forked from MarceloCajueiro/script.rb
Move sidekiq jobs from one queue to another
queue = Sidekiq::Queue.new("default")
queue.each do |job|
if job.klass == "DailyFrequencyCreatorWorker"
DailyFrequencyCreatorWorker.set(queue: 'daily_frequency_creator').perform_async(*job.args)
job.delete
end
end;nil
@scart88
scart88 / .Asof SQL Queries
Created February 23, 2024 23:27 — forked from tdfirth/.Asof SQL Queries
Asof joins in duckdb
How to think about asof queries.
@scart88
scart88 / db_backup_commands.md
Created July 31, 2023 00:43 — forked from AtulKsol/db_backup_commands.md
Commands to backup & restore database
  1. pg_dump is a nifty utility designed to output a series of SQL statements that describes the schema and data of your database. You can control what goes into your backup by using additional flags.
    Backup: pg_dump -h localhost -p 5432 -U postgres -d mydb > backup.sql

    Restore: psql -h localhost -p 5432 -U postgres -d mydb < backup.sql

    -h is for host.
    -p is for port.
    -U is for username.
    -d is for database.

@scart88
scart88 / mrsk.md
Created July 28, 2023 22:37 — forked from huksley/mrsk.md
mrsk - the missing manual

MRSK

This documentation adds important additions to the docs for mrsk deploy tool (see github.com/mrsked/mrsk)

Destination flag

You can use mrsk deploy --destination staging

This will read config/deploy.yml and config/deploy.staging.yml files, and also will read .env.staging file if it exists.

# UFW Commands to make your life eaiser
# Install UFW
sudo apt-get update
sudo apt-get install ufw
# View UFW Status
sudo ufw status
# Enable UFW
@scart88
scart88 / WireGuard_Setup.txt
Created December 11, 2022 00:30 — forked from chrisswanda/WireGuard_Setup.txt
Stupid simple setting up WireGuard - Server and multiple peers
Install WireGuard via whatever package manager you use. For me, I use apt.
$ sudo add-apt-repository ppa:wireguard/wireguard
$ sudo apt-get update
$ sudo apt-get install wireguard
MacOS
$ brew install wireguard-tools
Generate key your key pairs. The key pairs are just that, key pairs. They can be
@scart88
scart88 / application_helper.rb
Created March 11, 2022 11:26 — forked from mynameispj/application_helper.rb
Rails - Easy "active" classes for menu links in Rails
module ApplicationHelper
def current_class?(test_path)
return 'active' if request.path == test_path
''
end
end
@scart88
scart88 / replace_words.rb
Created March 11, 2022 11:26 — forked from jhjguxin/replace_words.rb
Read, edit, and write a text file line-wise using Ruby
# http://stackoverflow.com/questions/4397412/read-edit-and-write-a-text-file-line-wise-using-ruby
# files = Dir.glob(File.dirname(__FILE__)+"/../app/**/*.rb")
files = Dir[Rails.root.join("app", "**/*.rb")]
files.each do |f_name|
# puts f_name
File.open(f_name, "r+") do |f|
old_pos = 0
f.each do |line|
@scart88
scart88 / Img Alt Tag Scraper using Nokogiri
Created March 11, 2022 11:26 — forked from MurtzaM/Img Alt Tag Scraper using Nokogiri
Scrapes img alt tags from HTML using Ruby with Nokogiri gem
require 'nokogiri'
require 'open-uri'
#Scrapes img alt tags from specified URL
url = "CHANGE ME"
alt_tags = Array.new
doc = Nokogiri::HTML(open(url))
alt_tags = doc.css('img').map{ |i| i['alt'] }
#Prints out unique alt tags