Skip to content

Instantly share code, notes, and snippets.

@willkoehler
willkoehler / core_extensions.rb
Created May 25, 2015 02:06
Simple symbolized accessor for Active Record
class String
def articleize
%w(a e i o u).include?(self[0].downcase) ? "an #{self}" : "a #{self}"
end
end
@willkoehler
willkoehler / 1 hospital.rb
Last active August 29, 2015 14:00
Rails eager loading variations. The pure "Rails" way is ~5x slower than using raw SQL snippets.
class Hospital < ActiveRecord::Base
has_many :user_assignments, :dependent => :destroy
has_many :users, :through => :user_assignments
# 30 hospitals
# There are 36 columns in this table, so we want to use .select() to limit which ones are pulled by the query
end
@willkoehler
willkoehler / document_uploader.rb
Created February 11, 2014 15:14
Carrier uploader for documents that creates thumbnails from images and PDFs
class DocumentUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick
# Store on Amazon S3 using Fog
storage :fog
# Directory where uploaded files will be stored.
def store_dir
"documents/#{model.id}"
end