Skip to content

Instantly share code, notes, and snippets.

@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"
}
[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 / .tmux.conf
Created January 27, 2014 20:17
My tmux configuration
# use ctrl-a as the prefix (instead of the default ctrl-b)
set-option -g prefix C-a
# pres ctrl-a, ctrl-a to flip between windows
bind-key C-a last-window
# use vi keybinding (basically just hjkl for navigation)
set-window-option -g mode-keys vi
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection
# include ngGrid in our app:
@app = angular.module("breeze", ["... "ngGrid", ...])
# try to DI sortService from ngGrid
# (https://github.com/angular-ui/ng-grid/blob/8017eee035ac21ce9989eb26ea5dc6c13b9553cd/src/services/SortService.js)
ComplaintReportCtrl = ($scope, ComplaintReport, $sortService) ->
# then call it:
@davetapley
davetapley / has_many_inverse_of.rb
Created December 24, 2013 22:30
Code for Bi-directional associations in Rails 4.
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.0.0'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
@davetapley
davetapley / error
Last active December 30, 2015 23:49
FloatDomainError: Infinity when updating PostgreSQL range with infinity (ActiveRecord 4.0.x) and PostgreSQL (9.2)
1) Error:
RangeTest#test:
FloatDomainError: Infinity
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `to_r'
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `=='
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `=='
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `!='
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:97:in `_field_changed?'
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/dirty.rb:66:in `write_attribute'
/home/dave/.rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.0.1/lib/active_record/attribute_methods/write.rb:21:in `__temp__e657d6f52716e67656='
@davetapley
davetapley / a.sh
Created December 10, 2013 23:34
Vagrant failing to run Puppet if first `vagrant up` fails because another virtual box already has port 3000 (see: a.sh). Then succeeding after `destroy` then `up` again (see: b.sh).
# CLONE
dave@lexi:~/projects$ git clone https://github.com/rails/rails-dev-box.git
Cloning into 'rails-dev-box'...
remote: Counting objects: 267, done.
remote: Compressing objects: 100% (185/185), done.
remote: Total 267 (delta 79), reused 222 (delta 52)
Receiving objects: 100% (267/267), 80.74 KiB, done.
Resolving deltas: 100% (79/79), done.
@davetapley
davetapley / create_groups.rb
Last active December 26, 2015 11:49
Postgres array type sexiness in Rails 4
class CreateGroups < ActiveRecord::Migration
def change
create_table :groups do |t|
t.integer :user_id
t.string :name
t.string :emails, array: true, null: false, default: []
t.timestamps
end
add_index :groups, :user_id
WITH q_geocodes(name, id) AS (VALUES ('SF', 6096070),('NYC', 6164533))
SELECT
q_geocodes.name,
AVG(CAST(end_time + (time_zone * INTERVAL '1 sec') AS time)) AS mean_wake_time,
COUNT(events.*) AS counts,
histogram(EXTRACT(epoch FROM CAST(end_time + (time_zone * INTERVAL '1 sec') AS time)) :: real, EXTRACT(epoch FROM '04:00'::time) :: real, EXTRACT(epoch FROM '12:00'::time) :: real, 16) AS hist,
EXTRACT(DOW FROM (end_time + time_zone * INTERVAL '1 sec' )) AS dow
@davetapley
davetapley / agg_histogram.sql
Created September 15, 2013 18:29
Normalized histogram in psql
-- from: https://wiki.postgresql.org/wiki/Aggregate_Histogram
CREATE OR REPLACE FUNCTION hist_sfunc (state INTEGER[], val REAL, min REAL, max REAL, nbuckets INTEGER) RETURNS INTEGER[] AS $$
DECLARE
bucket INTEGER;
i INTEGER;
BEGIN
-- width_bucket uses nbuckets + 1 (!) and starts at 1.
bucket := width_bucket(val, min, max, nbuckets - 1) - 1;