Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Executable Testcase for https://github.com/rails/rails/issues/8020#issuecomment-19958061 Note that this bug is gone in AR 4.0.0
gem 'activerecord', '3.2.13'
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
end
create_table :comments do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class MarshallingTest < MiniTest::Unit::TestCase
def setup
Post.create!
Post.create!
end
def teardown
Post.destroy_all
end
def test_marshalling_with_touching_associations
posts = Post.all
posts.each do |post|
post.comments
end
marshalled = Marshal.load(Marshal.dump(posts))
assert_equal Post.all, marshalled
end
def test_marshalling_without_touching_associations
posts = Post.all
marshalled = Marshal.load(Marshal.dump(posts))
assert_equal Post.all, marshalled
end
def test_marshalling_with_arrayifying_associations
posts = Post.all
posts.each do |post|
post.comments.to_a
end
marshalled = Marshal.load(Marshal.dump(posts))
assert_equal Post.all, marshalled
end
def test_dumping_twice
posts = Post.all
posts.each do |post|
post.comments.to_a
end
Marshal.dump(posts) # as noted by a commenter, dumping twice helps...
marshalled = Marshal.load(Marshal.dump(posts))
assert_equal Post.all, marshalled
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.