Skip to content

Instantly share code, notes, and snippets.

View benoittgt's full-sized avatar
🏴

Benoit Tigeot benoittgt

🏴
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

Ruby: The future of frozen string literals

What is a literal?

In programming languages, literals are textual representations of values in the source code. This is a syntactical concept.

Some examples:

7 # integer literal
@firedev
firedev / reload.js
Last active October 3, 2024 08:42
Turbo Stream Actions
// <turbo-stream action="reload" target="frame-id"></turbo-stream>
StreamActions.reload = function () {
document.getElementById(this.getAttribute("target"))?.reload()
}
# make sure all expect wait for turbo before expecting
module RSpec
module Matchers
expect_old = instance_method(:expect).bind(self)
define_method(:expect) do |*args, &block|
expect_old.call(page).not_to have_selector('html[aria-busy="true"]')
expect_old.call(page).not_to have_selector('turbo-frame[busy]')
expect_old.call(*args, &block)
end
end
@leandronsp
leandronsp / server.rb
Created February 15, 2023 11:39
HTTP server using Ractors (Ruby 3)
require 'socket'
@queue = Ractor.new do
loop do
Ractor.yield(Ractor.receive, move: true)
end
end
listener = Ractor.new(@queue) do |queue|
socket = TCPServer.new(PORT)
@harry-wood
harry-wood / installing libxml on mac for ruby.md
Last active February 5, 2025 09:10
Installing libxml on mac for ruby.md

Installing/debugging libxml on mac for ruby/rails

Install xcode command line tools (or check if they're already installed)

xcode-select --install

Do we have libxml installed already?

xsltproc --version
@nikvdp
nikvdp / atuin.zsh
Created August 18, 2022 14:44
Use atuin to power ctrl-r history search but with fzf. Also disable atuin's up arrow bindings and use ctrl-e to bring up atuin's own tui
# make sure you have `tac` [1] (if on on macOS) and `atuin` [2] installed, then drop the below in your ~/.zshrc
#
# [1]: https://unix.stackexchange.com/questions/114041/how-can-i-get-the-tac-command-on-os-x
# [2]: https://github.com/ellie/atuin
atuin-setup() {
! hash atuin && return
bindkey '^E' _atuin_search_widget
export ATUIN_NOBIND="true"
@minmax
minmax / pg_index_progress.sql
Created January 18, 2022 21:44
pg index progress
-- https://gitlab.com/-/snippets/2138417
select
now(),
query_start as started_at,
now() - query_start as query_duration,
format('[%s] %s', a.pid, a.query) as pid_and_query,
index_relid::regclass as index_name,
relid::regclass as table_name,
(pg_size_pretty(pg_relation_size(relid))) as table_size,
nullif(wait_event_type, '') || ': ' || wait_event as wait_type_and_event,
@searls
searls / whereable.rb
Created September 4, 2021 16:06
The initial implementation of a Whereable query filter for KameSame.
class Whereable
def initialize(where:, model: Item, ranking_conditions: [], valid: true, data_source: nil)
@model = model
@where = where
@data_source = data_source
@ranking_conditions = ranking_conditions
@valid = valid
end
def valid?
@searls
searls / 001_functions_and_views.sql
Created December 29, 2020 13:32
KameSame's December 2020 search overhaul
-- This all starts with some functions and a *materialized* postgres view that unnests several
-- arrays of strings of definitions into flattened rows that are easier to search. Fun fact:
-- you can even create indexes on materialized views' columns! They'll refresh whenever the view
-- is refreshed (which in my case is every time that we pull new dictionary data from WaniKani or JMDICT
-- This function will take an array of strings and convert all the double-width alphanumeric characters
-- and normalize them as half-width. That way a search query can be massaged from "OK" to "ok" easily
CREATE OR REPLACE FUNCTION array_hankakufy_romaji(character varying[])
RETURNS character varying[]
AS