Created
March 20, 2012 02:35
-
-
Save rywall/2130349 to your computer and use it in GitHub Desktop.
Only store in IM if all attributes are present
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'rails', :git => 'git://github.com/rails/rails.git', :ref => 'c1f397f82c7b3f352500832a399b5dbdc500b9c8'# change as required | |
require 'active_record' | |
require 'logger' | |
# Show ActiveRecord log output | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# Print out what version we're running | |
puts "Active Record #{ActiveRecord::VERSION::STRING}" | |
# Connect to an in-memory sqlite3 database (more on this in a moment) | |
ActiveRecord::Base.establish_connection( | |
:adapter => 'sqlite3', | |
:database => ':memory:' | |
) | |
# Create the minimal database schema necessary to reproduce the bug | |
ActiveRecord::Schema.define do | |
create_table :users, :force => true do |t| | |
t.string :name | |
end | |
end | |
# Create the minimal set of models to reproduce the bug | |
class User < ActiveRecord::Base | |
end | |
# Small patch to Identity Map | |
module ActiveRecord::IdentityMap | |
class << self | |
def add(record) | |
repository[record.class.symbolized_sti_name][record.id] = record if record.class.column_names == record.attributes.keys | |
end | |
end | |
end | |
# Create some test data | |
# | |
# If you're demonstrating an exception, then this is probably not necessary, | |
# but if your bug is to do with the wrong data being returned from the database, | |
# then you'll probably need some test data to show that. | |
User.create(:name => "Ryan") | |
# Demonstrate the bug fix | |
ActiveRecord::IdentityMap.use do | |
User.select(:id).find(1) | |
User.find(1).name | |
User.select(:id).find(1) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment