Created
May 2, 2013 22:14
-
-
Save adamcooper/5505897 to your computer and use it in GitHub Desktop.
This file contains 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', '3.2.13' # change as required | |
require 'active_record' | |
# 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| | |
end | |
create_table :user_folders, :force => true do |t| | |
t.integer :user_id | |
end | |
end | |
# Create the minimal set of models to reproduce the bug | |
class User < ActiveRecord::Base | |
has_many :user_folders | |
end | |
class UserFolder < ActiveRecord::Base | |
belongs_to :user | |
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 = User.create! | |
user_folder = UserFolder.create(:user => user) | |
# Reproduce the actual bug! | |
user = User.first | |
puts "initial assocation is not loaded: #{user.user_folders.loaded?}" | |
puts "loading first_folder object out with 'user.user_folders.first'" | |
first_folder = user.user_folders.first | |
puts "user.user_folders.loaded? - #{user.user_folders.loaded?}" | |
puts "getting the first_folder_again via 'user.user_folders.first'" | |
first_folder_again = user.user_folders.first | |
puts "user.user_folders.loaded? - #{user.user_folders.loaded?}" | |
puts "first_folder == first_folder_again - #{first_folder == first_folder_again}" | |
puts "first_folder.object_id == first_folder_again.object_id - #{first_folder.object_id == first_folder_again.object_id}" | |
puts "user.user_folders.loaded?: #{user.user_folders.loaded?}" | |
puts "\n\nSTARTING OVER BUT LOADING THE ASSOCIATION FIRST\n\n" | |
user = User.first | |
puts "loading initial user_folders"; | |
user.user_folders[0] | |
puts "initial assocation is not loaded: #{user.user_folders.loaded?}" | |
puts "loading first_folder object out with 'user.user_folders.first'" | |
first_folder = user.user_folders.first | |
puts "user.user_folders.loaded? - #{user.user_folders.loaded?}" | |
puts "getting the first_folder_again via 'user.user_folders.first'" | |
first_folder_again = user.user_folders.first | |
puts "user.user_folders.loaded? - #{user.user_folders.loaded?}" | |
puts "first_folder == first_folder_again - #{first_folder == first_folder_again}" | |
puts "first_folder.object_id == first_folder_again.object_id - #{first_folder.object_id == first_folder_again.object_id}" | |
This file contains 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
Active Record 3.2.13 | |
-- create_table(:users, {:force=>true}) | |
-> 0.0137s | |
-- create_table(:user_folders, {:force=>true}) | |
-> 0.0005s | |
initial assocation is not loaded: false | |
loading first_folder object out with 'user.user_folders.first' | |
user.user_folders.loaded? - false | |
getting the first_folder_again via 'user.user_folders.first' | |
user.user_folders.loaded? - false | |
first_folder == first_folder_again - true | |
first_folder.object_id == first_folder_again.object_id - false | |
user.user_folders.loaded?: false | |
STARTING OVER BUT LOADING THE ASSOCIATION FIRST | |
loading initial user_folders | |
initial assocation is not loaded: true | |
loading first_folder object out with 'user.user_folders.first' | |
user.user_folders.loaded? - true | |
getting the first_folder_again via 'user.user_folders.first' | |
user.user_folders.loaded? - true | |
first_folder == first_folder_again - true | |
first_folder.object_id == first_folder_again.object_id - true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment