Skip to content

Instantly share code, notes, and snippets.

@iamvery
iamvery / PostgreSQL_deferrable_foreign_key.sql
Created January 5, 2011 16:16
Allows foreign key to be deferred during a transaction, but it will be applied immediately if not deferred.
ALTER TABLE "TableName"
ADD CONSTRAINT "ConstraintName" FOREIGN KEY ("columnName")
REFERENCES "AnotherTableName" ("anotherColumnName") MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY IMMEDIATE;
-- I generally use the form "TableName_fk_constraintname" for foreign key constraint names (for reference)
-- This particular foreign key will cascade on update and delete, but those particular directives aren't required. (for reference)
@iamvery
iamvery / jquery-ujs_remote_form_example.js
Created January 6, 2011 17:34
Successful AJAX handler on remote form using jquery-ujs (rails.js)
$('form').bind('ajax:success', function(e, data){
alert(data);
});
@iamvery
iamvery / custom_gem_path_rails3.rb
Created January 14, 2011 20:31
This actually should probably belong at the top of your config.ru file. The purpose is to change the GEM_HOME environment variable and then tell RubyGems to look there for gems for now on.
# Specify your own GEM_HOME, but keep the standard gem path in GEM_PATH
ENV['GEM_HOME'] = "#{ENV['HOME']}/.gems"
ENV['GEM_PATH'] = "#{ENV['GEM_HOME']}:/usr/lib/ruby/gems/1.8"
require 'rubygems'
Gem.clear_paths
@iamvery
iamvery / load_database_from_file.bat
Created January 18, 2011 15:44
Quick reminder to mysql of the command used at the windows command line to load a SQL backup into an empty database.
# psql.exe is generally found in C:\Program Files [(x86)]\PostgreSQL\PG_VERSION\bin\
psql.exe DATABASE_NAME < C:\LOCATION_TO_BACKUP\backup.sql
@iamvery
iamvery / 3-letter-word.rb
Created April 14, 2011 20:04
Generate a random 3 letter acronym. Doesn't guarantee uniqueness, but this has to be the governments best friend :)
Array.new(3).map { ('A'..'Z').to_a.sample }.join
def takes_and_has_opts(*args)
opts = args.extract_options!
lambda do |*args|
o = args.extract_options!
o.merge!(opts)
o
end
end
@iamvery
iamvery / osx-restart-cisco-vpn.sh
Created December 7, 2011 04:46
Restart the Cisco VPN service in Mac OS 10.6ish
# See https://discussions.apple.com/thread/3173994?start=0&tstart=0
sudo launchctl unload /System/Library/LaunchDaemons/com.apple.racoon.plist
sudo launchctl load /System/Library/LaunchDaemons/com.apple.racoon.plist
@iamvery
iamvery / fix-mysql2-gem-lib.sh
Created December 19, 2011 19:28
"Fix" mysql2 gem installation on Mac OSX
# change this variable to the appropriate libmysqlclient lib version
VERSION=18
# change this variable to the path to your mysql2 gem
MYSQL_GEM_PATH=~/.rvm/gems/ruby-1.9.2-p180/gems/mysql2-0.2.7
sudo install_name_tool -change libmysqlclient.$VERSION.dylib /usr/local/mysql/lib/libmysqlclient.$VERSION.dylib $MYSQL_GEM_PATH/lib/mysql2/mysql2.bundle
@iamvery
iamvery / multiple_ordering_with_condition.md
Created March 8, 2012 16:36
Multiple ordering with condition on associated records

** This issue came up in a Rails 3.0.11 app **

Article Model - the good parts

has_many :categorizations
has_many :categories, :through => :categorizations

scope :not_news, includes(:categories).where(Category.arel_table[:name].not_eq('news'))
scope :by_date,  order('articles.post_date DESC, articles.id DESC')
@iamvery
iamvery / safe_cast_str_to_int.sql
Created May 8, 2012 18:53
PostgreSQL function that safely casts a string value to an integer. Useful when sorting string fields as numbers :)
-- Function: "Safe_Cast_Str_to_Int"(character varying, integer)
CREATE OR REPLACE FUNCTION "Safe_Cast_Str_to_Int"(input_string character varying, default_value integer DEFAULT NULL::integer)
RETURNS integer AS
$BODY$
BEGIN
BEGIN
RETURN input_string::integer;
EXCEPTION WHEN data_exception THEN
RETURN default_value;