Skip to content

Instantly share code, notes, and snippets.

View Envek's full-sized avatar

Andrey Novikov Envek

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

@charlie-wasp
charlie-wasp / obfuscated_id.rb
Created August 15, 2018 15:00
Obfuscated ID generator
module ObfuscatedId
extend ActiveSupport::Concern
# Constants used to obfuscate identifiers with formula id * multipier % modulo
# modulo and multiplier must be coprime numbers to avoid collisions
# modulo must be larger than maximum value of id
MODULO = Rails.application.secrets.id_obfuscation[:modulo]
EXPONENT = Rails.application.secrets.id_obfuscation[:exponent]
# Should contain digits and letters common for latin and cyrillic alphabets
# "12345679AEKMHPCTYX".split('').shuffle.join
@yyscamper
yyscamper / jsonb_remove_keys.sql
Created February 26, 2018 09:49
PostgreSQL: Remove Multiple Keys From JSONB
CREATE OR REPLACE FUNCTION jsonb_remove_keys(
jdata JSONB,
keys TEXT[]
)
RETURNS JSONB AS $$
DECLARE
result JSONB;
len INT;
target TEXT;

Demo:

Spoiler warning

Spoiler text. Note that it's important to have a space after the summary tag. You should be able to write any markdown you want inside the <details> tag... just make sure you close <details> afterward.

console.log("I'm a code block!");
@whizzzkid
whizzzkid / XPS-15 9560 Getting Nvidia To Work on KDE Neon
Last active December 3, 2022 15:43
[XPS 15 Early 2017 9560 kabylake] Making Nvidia Drivers + (CUDA 8 / CUDA 9 / CUDA 9.1) + Bumblebee work together on linux ( Ubuntu / KDE Neon / Linux Mint / debian )
# Instructions for 4.14 and cuda 9.1
# If upgrading from 4.13 and cuda 9.0
$ sudo apt-get purge --auto-remove libcud*
$ sudo apt-get purge --auto-remove cuda*
$ sudo apt-get purge --auto-remove nvidia*
# also remove the container directory direcotory at /usr/local/cuda-9.0/
# Important libs required with 4.14.x with Cuda 9.X
$ sudo apt install libelf1 libelf-dev
@vzaliva
vzaliva / set_lang.py
Last active April 16, 2022 09:14
Script to toggle between the two last used language in Gnome
#!/usr/bin/env python3
#
# Script to toggle between the two last used language in Gnome
#
# History:
#
# Original script by Jacob Vlijm
# Saving last language and togging: Vadim Zaliva
#
# See also: http://askubuntu.com/questions/871678/how-can-i-quickly-switch-between-two-out-of-multiple-languages
@letorbi
letorbi / fontsmoothie.js
Last active January 15, 2022 17:39
This code enforces font-smothing for web fonts even if it's not enabled in the system settings. More info: http://pixelsvsbytes.com/blog/2013/02/nice-web-fonts-for-every-browser
// Font Smoothie copyright 2013,14,15 Torben Haase <http://pixelsvsbytes.com>
// Source-URL <https://gist.github.com/letorbi/5177771>
//
// Font Smoothie is free software: you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option) any
// later version.
//
// Font Smoothie is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
@tlowrimore
tlowrimore / union_scope.rb
Last active January 13, 2023 21:12
Unions multiple scopes on a model, and returns an instance of ActiveRecord::Relation.
module ActiveRecord::UnionScope
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
def union_scope(*scopes)
id_column = "#{table_name}.#{primary_key}"
sub_query = scopes.map { |s| s.select(id_column).to_sql }.join(" UNION ")
where "#{id_column} IN (#{sub_query})"
@jingoro
jingoro / modular_arithmetic.rb
Created April 14, 2012 23:43
Ruby modular arithmetic
# Some modular arithmetic helper methods useful for number theory or
# cryptography.
#
# # Examples
#
# Compute the greatest common denominator of 114 and 48:
#
# gcd(114, 48) #=> 6
#
# Compute `a` and `b` such that `a*x + b*y = gcd(x, y)`:
@chetan
chetan / yardoc_cheatsheet.md
Last active August 26, 2024 13:48
YARD cheatsheet