Skip to content

Instantly share code, notes, and snippets.

View agraves's full-sized avatar

Aaron Graves agraves

  • New York, NY
View GitHub Profile
- create_table "book_authors", :force => true do |t|
+ create_table "book_authors", :id => false, :force => true do |t|
+ t.integer "id", :null => false
@agraves
agraves / surprise.rb
Created February 7, 2012 22:48
surprise
# incorrect
describe 'foo' do
it 'should bar' do
@foo = Factory(:foo)
@foo.bar
@foo.should_receive :bar # wrong
end
end
# correct
@agraves
agraves / application_controller.rb
Created January 26, 2012 07:21
Trace redirects
# Source: http://jkfill.com/2011/05/13/log-which-line-called-redirect_to/
#
# Toss this in ApplicationController and all redirects will generate a log statement
unless Rails.env.production?
def redirect_to(options = {}, response_status = {})
::Rails.logger.error("Redirected by #{caller(1).first rescue "unknown"}")
super(options, response_status)
end
end
@agraves
agraves / gotcha.rb
Created January 26, 2012 00:48
Spot the bug
class Foo
def self.foo
'foo'
end
private
def self.bar
'bar'
end
@agraves
agraves / group.rb
Created August 1, 2011 16:27
Rewrite has_many association on the fly
# Ever needed to dynamically rewrite the conditions on a has_many association?
# This is definitely a hack, but useful if parameterizing or adding scopes are
# not practical for whatever reason.
class Group < ActiveRecord::Base
# Any association will do
has_many :people
def filter_people(conditions)
# All calls to this method after the association is traversed and before a reload