Skip to content

Instantly share code, notes, and snippets.

View solnic's full-sized avatar

Peter Solnica solnic

View GitHub Profile
@solnic
solnic / dm1.txt
Created December 4, 2012 12:57
dm1 vs dm2 - eager loading association
1.9.3p327 :011 > Benchmark.bm { |x| x.report { User.all.each { |u| u.city } } }
user system total real
4.320000 0.010000 4.330000 ( 4.344362)
@solnic
solnic / rvm_remove_old.sh
Created December 4, 2012 09:34
Remove all rubies older than given date
DATE=2012-06-01
for version in `find $rvm_path/rubies/ -mindepth 1 -maxdepth 1 -type d -not -newermt $DATE | cut -d / -f 7`; do rvm remove $version; done
SELECT DISTINCT "song_id", "name", "tag_name"
FROM (
SELECT * FROM (
SELECT "id" AS "song_id", "name" FROM "songs"
) AS "left" NATURAL JOIN (
SELECT * FROM "song_tags" AS "left"
NATURAL JOIN (
SELECT "id" AS "tag_id", "name" AS "tag_name" FROM "tags"
) AS "right"
) AS "right"
/* generated */
SELECT "user_id", "username", "id", "street" FROM (SELECT "id" AS "user_id", "username" FROM "users") AS "left" NATURAL JOIN "addresses" AS "right"
/* inner join alternative */
SELECT users.id AS user_id, username, addresses.id, street FROM users INNER JOIN addresses ON users.id = addresses.user_id
@solnic
solnic / example.rb
Created September 26, 2012 17:18
Mapper generator using Virtus model
class User
include DataMapper::Model
attribute :id, Integer
attribute :name, String
attribute :age, Integer
end
DataMapper.generate_mapper_for(User) do
key :id
@solnic
solnic / log.txt
Created August 19, 2012 10:03 — forked from steveklabnik/log.txt
A fun shell script from #euruku
$ history | awk {'print $2, $3, $4'} | sort | uniq -c | sort -k1 -rn | head -n 30
610 git status
568 git commit -m
491 git add .
252 git push origin
176 bundle
138 rails s
128 ls
120 git commit --amend
114 git reset --hard
@solnic
solnic / route_name.rb
Created July 26, 2012 10:22
Determine current route name in Rails
def route_name
Rails.application.routes.recognize(request) { |route, _| route.name }.flatten.last.name.to_sym
end
@solnic
solnic / virtus-coercion.rb
Created May 11, 2012 22:00 — forked from avdi/virtus-coercion.rb
ValueObject in Virtus with a custom constructor
require 'virtus'
class Point
include Virtus::ValueObject
attribute :x, Integer
attribute :y, Integer
def initialize(value)
self.x = value[0]
class User
include Virtus
attribute :address do
attribute :street, String
attribute :city, String
attribute :zipcode, String
end
end
@solnic
solnic / osx_rails_env_setup_in_6_steps.md
Created March 29, 2012 12:33
6 steps to get up'n'running with Rails on OS X

6 steps to set up a rails development environment on OS X:

  • install Apple Command Line Tools
  • install homebrew[1]: /usr/bin/ruby -e "$(/usr/bin/curl -fksSL https://raw.github.com/mxcl/homebrew/master/Library/Contributions/install_homebrew.rb)"
  • install rvm: bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
  • install ruby rvm install 1.9.3
  • install rails