Instantly share code, notes, and snippets.

View Gemfile
gem 'lograge' # more readable logs
gem 'logstash-event' # for logstash json format
gem 'mono_logger' # threadsafe logging
View polymorphic_many_to_many_in_rails.md

This was my solution for a polymorphic many-to-many association

class ItemCountry < ActiveRecord::Base
  belongs_to :locatable, :polymorphic => true
  belongs_to :country
  # fields are :locatable_id, :locatable_type, :country_id
end

class Title < ActiveRecord::Base
  has_many :countries, :through => :item_countries, :as => :locatable
View bin-squash_migrations
#!/usr/bin/env bash
git ls-files db/migrate/*.rb | sort | tail -1 | \
ruby -e "schema_version=STDIN.read[/\d+/]; init_schema=%(db/migrate/#{schema_version}_init_schema.rb);
%x(git rm -f db/migrate/*.rb;
mkdir db/migrate;
git mv db/schema.rb #{init_schema};
rake db:migrate;
git add db/schema.rb; git commit -m 'Squashed migrations')"
View rails32_http_patch_support.rb
# Rails 3.2 support for HTTP PATCH.
fail "Remove this patch" if Rails::VERSION::MAJOR > 3
# see http://weblog.rubyonrails.org/2012/2/26/edge-rails-patch-is-the-new-primary-http-method-for-updates/
# https://github.com/rails/rails/pull/505
# Be very conservative not to monkey-patch any methods until
# the relevant files are loaded.
ActiveSupport.on_load(:action_controller) do
ActionDispatch::Request.instance_eval do
# Is this a PATCH request?
View interview_questions.txt
Good Interview Questions
"Give Him/Her the opportunity to impress you, and maybe even give you a revelation"
Ask open-ended questions, see
---> attitudes about the topic
---> how s/he thinks
---> what s/he's been thinking about lately
Q: Compare Ruby to another language.
View jdbc_sample.rb
require './lib/adsjdbc-10.10.0.28.jar'
java_import java.sql.Driver
java_import java.sql.DriverManager
java_import 'com.extendedsystems.jdbc.advantage.ADSDriver'
class AdsAdapter
def initialize(connect_string)
@connect_string = connect_string
end
View comment.md
View errors.html.erb
<% if Rails.env.development? || Subdomain.new.internal_ip_user?(request) %>
More verbose errors
<% end %>
View Gemfile
# A sample Gemfile
source "https://rubygems.org"
gem 'mechanize'