Skip to content

Instantly share code, notes, and snippets.


Adam Lassek alassek

  • Flywheel
  • Omaha, NE
View GitHub Profile

An ActiveRecord conundrum

I'm going to describe a weird problem I faced when using ActiveRecord today. To protect the innocent, I'm not going to talk about the app I'm actually working on but will instead discuss a hypothetical but isomorphic database design for a clone of the popular blogging platform Tumblr.

Tumblr lets you publish various different sorts of content. We might be tempted to shove all these types in a big STI table, but the types are all quite different from one another and so we give them their own tables.

rietta / plpgsql.rake
Last active Aug 8, 2020
Are you using PostgreSQL and don't want to make your app run as PostgreSQL super user, then add this custom rake task to your `lib/tasks` folder and be happy.
View plpgsql.rake
# PostgreSQL writes two optional commands to the database schema
# file, called db/structure.sql, that can only be run as a root
# database user. These are not needed actually, so comment them
# out automatically
# COMMENT ON EXTENSION plpgsql IS 'PL/pgSQL procedural language';
namespace :db do
ernie / delegate.rb
Last active May 31, 2016
An alternate take on the delegation class macro provided by ActiveSupport. Updated with Ruby 2.0's caller_locations.
View delegate.rb
#!/usr/bin/env ruby
class Module
def delegate(*args)
dest, prefix = _extract_valid_delegation_options(args.pop)
_define_delegators(caller_locations.first, prefix, dest, args)

Falsehoods programmers believe about prices

  1. You can store a price in a floating point variable.
  2. All currencies are subdivided in 1/100th units (like US dollar/cents, euro/eurocents etc.).
  3. All currencies are subdivided in decimal units (like dinar/fils)
  4. All currencies currently in circulation are subdivided in decimal units. (to exclude shillings, pennies) (counter-example: MGA)
  5. All currencies are subdivided. (counter-examples: KRW, COP, JPY... Or subdivisions can be deprecated.)
  6. Prices can't have more precision than the smaller sub-unit of the currency. (e.g. gas prices)
  7. For any currency you can have a price of 1. (ZWL)
  8. Every country has its own currency. (EUR is the best example, but also Franc CFA, etc.)
View associations.rb
# use inverse_of to 'just work'
class User < ActiveRecord::Base
has_many :contributions, inverse_of: :user
has_many :posts, through: :contributions
class Post < ActiveRecord::Base
has_many :contributions, inverse_of: :post
has_many :contributors, through: :contributions,
source: :user
carlzulauf / thin.rb
Last active Dec 17, 2015
Thin initd script. Spawns thin daemons from a config directory using a specific non-privileged user (`carl`).
View thin.rb
#!/usr/bin/env ruby
# thin init script
# install to /etc/init.d/thin
# crazy debian LSB header stuff
# Provides: thin
# Required-Start: $local_fs $remote_fs
# Required-Stop: $local_fs $remote_fs
dergachev /
Last active Nov 23, 2022
OS X Screencast to animated GIF

OS X Screencast to animated GIF

This gist shows how to create a GIF screencast using only free OS X tools: QuickTime, ffmpeg, and gifsicle.

Screencapture GIF


To capture the video (filesize: 19MB), using the free "QuickTime Player" application:

avdi / selective_method_import.rb
Created Jan 16, 2013
Playing around with selective method import in Ruby
View selective_method_import.rb
# Generate a module which imports a given subset of module methods
# into the including module or class.
def Methods(source_module, *method_names)
all_methods = source_module.instance_methods +
unwanted_methods = all_methods - method_names
import_module = source_module.clone
import_module.module_eval do
define_singleton_method(:to_s) do
"ImportedMethods(#{source_module}: #{method_names.join(', ')})"
ryanb /
Created Nov 29, 2012
Points on how modules can make code difficult to read.

My issues with Modules

In researching topics for RailsCasts I often read code in Rails and other gems. This is a great exercise to do. Not only will you pick up some coding tips, but it can help you better understand what makes code readable.

A common practice to organize code in gems is to divide it into modules. When this is done extensively I find it becomes very difficult to read. Before I explain further, a quick detour on instance_eval.

You can find instance_eval used in many DSLs: from routes to state machines. Here's an example from Thinking Sphinx.

class Article &lt; ActiveRecord::Base
Darksecond / nginx.conf
Created Oct 8, 2012
Systemd + Unicorn + Nginx + no-downtime-reload
View nginx.conf
# The only setting we feel strongly about is the fail_timeout=0
# directive in the "upstream" block. max_fails=0 also has the same
# effect as fail_timeout=0 for current versions of nginx and may be
# used in its place.
# you generally only need one nginx worker unless you're serving
# large amounts of static files which require blocking disk reads
worker_processes 1;
# # drop privileges, root is needed on most systems for binding to port 80