Skip to content

Instantly share code, notes, and snippets.

[4] pry(main)> User.all.second
User Load (42.5ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL
=> #<User id: 2, ...>
[5] pry(main)> User.all.offset(1).limit(1)
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."deleted_at" IS NULL LIMIT 1 OFFSET 1
=> [#<User id: 2, ...]
@davetapley
davetapley / json_stringify.json
Last active August 29, 2015 13:58
Access array show in object by util.inspect, but not JSON.stringify
{
name: "error",
length: 97,
severity: "ERROR",
code: "42P01",
position: "13",
file: "parse_relation.c",
line: "873",
routine: "parserOpenTable"
}
@davetapley
davetapley / ab.sh
Created April 17, 2014 21:28
Failing to call res.send(200) in an express app
$ ab -n 1000 -c 1000 http://localhost:3000/hang
This is ApacheBench, Version 2.3 <$Revision: 1554214 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking localhost (be patient)
apr_socket_recv: Connection reset by peer (104)
test=# CREATE TABLE tzs(with_tz TIMESTAMP WITH TIME ZONE, without_tz TIMESTAMP WITHOUT TIME ZONE, comment TEXT);
CREATE TABLE
test=# INSERT INTO tzs VALUES( now(), now(), 'now()');
test=# INSERT INTO tzs VALUES( timezone('America/New_York', now()), timezone('America/New_York', now()), 'timezone(New York, now())');
INSERT 0 1
test=# INSERT INTO tzs VALUES( '2014-05-06T17:14:00Z', '2014-05-06T17:14:00Z', 'ISO 8601 Zulu' );
INSERT 0 1
test=# INSERT INTO tzs VALUES( '2014-05-06T17:14:00-04:00', '2014-05-06T17:14:00-04:00', 'ISO 8601 offset' );
INSERT 0 1
test=# SELECT * FROM tzs ;
@davetapley
davetapley / array_iter.rb
Created June 12, 2014 18:00
Array Iter in Ruby
class ArrayIter < Array
class Iter < SimpleDelegator
attr_reader :next
attr_reader :prev
end
def <<(other)
last.instance_variable_set(:@next, other) if last
other_iter = Iter.new other
@davetapley
davetapley / clustering.txt
Last active August 29, 2015 14:02
Clustering
Start with a list of clusters:
a, b, x, c, x, x, d, e, f, g, x, h
Select out clusters to try and find a place for:
a, b, c, d, e, f, g, h
Map over a method to (optionally) assign a place (pN) to each cluster:
(this is done with a DelegateClass containing the place or nil)
@davetapley
davetapley / considating_ranges.md
Last active August 29, 2015 14:02
Consolidating ranges across a union in Postgresql

Two tables share a user_id, a tstzrange called period, then each have another column called category and place_id respectively. Both have constraints allowing a user to only have one 'other column' value at any time:

 user_id    | integer                  | not null
 period     | tstzrange                | not null
 category   | character varying(16)    |

    "category_events_user_id_period_excl" EXCLUDE USING gist (user_id WITH =, period WITH &&)
@davetapley
davetapley / pg.sql
Last active August 29, 2015 14:03
Postgres range empty check, with inclusive/exclusive end bound
test=# SELECT isempty('[0,0]' :: int4range);
isempty
---------
f
(1 row)
test=# SELECT isempty('[0,0)' :: int4range);
isempty
---------
t
@davetapley
davetapley / indexes.sql
Created July 22, 2014 03:46
Implicit primary key index is redundant
"events_pkey" PRIMARY KEY, btree (user_id, period)
"events_user_id_period_excl" EXCLUDE USING gist (user_id WITH =, period WITH &&)
@davetapley
davetapley / sequel_pk_in_module.rb
Created July 22, 2014 04:38
Explicitly declaring Sequel PK in module
def self.included(o)
o.extend ClassMethods
o.set_primary_key [:user_id, :period]
o.unrestrict_primary_key
end