Skip to content

Instantly share code, notes, and snippets.

@bricker
bricker / 2012071901_interpolation_shorthand.mdown
Created July 19, 2012 23:21
Accidental Discovery: Interpolation shorthand in Ruby

Accidental Discovery: Interpolation shorthand in Ruby

July 19, 2012

I type in Textmate all day long. One of things its Ruby bundle does is auto-complete the string interpolation syntax for you. If you're inside of double-quotes (or a few other cases where string interpolation is possible), and you hit the # (hash) key, it writes #{} and places your cursor between the curly-brackets.

So, sometimes when I'm testing things in IRB, I forget that it doesn't auto-complete things like Textmate does. The result? Accidentally discovering a shorthand for string interpolation:

@name = "World"
puts "Hello #@name."
@bricker
bricker / 2012073001_rails_dbsync.mdown
Created July 30, 2012 22:08
Rails: Synchronizing your development database with your production database using Rake tasks

Rails: Synchronizing your development database with your production database using Rake tasks

July 30, 2012

If you run an active website with lots of new content every day, it's often helpful to keep your local database up-to-date with your production database, so when you're working on the application locally you're working with fresh content.

Here are a couple rake tasks that you can copy-and-paste into your local Rails application which will make this syncing process a one-step process.

@bricker
bricker / 2012070101_making_django_and_rails_play_nice.mdown
Created July 30, 2012 22:43
Making Django and Rails play nice: Part 2 (part 2)

Making Django and Rails play nice: Part 2 (part 2)

July 1, 2012

A few months ago, former KPCC developer Eric Richardson posted a fantastic solution to a seemingly simple problem: How do you share sessions between a Rails application and a Django application?

The issue: Ruby and Python serialize objects using different libraries (Marshal and Pickle, respectively).

class Something
private
def words
"Hello"
end
end
class Else < Something
end
@bricker
bricker / 2012-09-07-thought_on_auto_expiring_cache_keys.markdown
Created September 8, 2012 05:56
Auto-Expiring cache keys in Rails - easy, but what about performance?

Auto-Expiring cache keys in Rails - easy, but what about performance?

September 7th, 2012

This was sparked by this post by DHH: http://37signals.com/svn/posts/3113-how-key-based-cache-expiration-works

At first I was excited to read about a new method I hadn't seen before, ActiveRecord's cache_key. It seemed like I was going to restructure our entire cache strategy to take advantage of this cool technique.

@blogs.blog_entries
# => error
module Validations
module ContentValidations
extend ActiveSupport::Concern
included do
validates :slug,
presence: true,
uniqueness: { scope: :published_at, message: "was already used for the published_at date."},
format: { with: %r{[\w-]+}}
#
module Concern
def modname
self.name
end
end
module Validation
extend Concern
module EventValidation; end
@bricker
bricker / unique_by_date_validator.rb
Created September 14, 2012 07:16
Makes sure an attribute is unique by a specified day, month, year, etc.
##
# Unique By validator
# Makes sure an attribute is unique by a specified day, month, year, etc.
# This validator is heavily based on ActiveRecord::Validations::UniquenessValidator
#
# It uses the `beginning_of_*` methods to fake the extraction of the filter (day, month, etc.)
# ActiveSupport::CoreExtensions::Date::Calculations
# http://apidock.com/rails/ActiveSupport/CoreExtensions/Date/Calculations
#
# `scope` must be a datetime field
# create_table "areas" to |t|
# t.string :title
# t.integer :parent_id
# end
class Area
has_many :children, class_name: "Area", foreign_key: "parent_id"
belongs_to :parent
end