Skip to content

Instantly share code, notes, and snippets.

@dugsmith
Created June 9, 2014 22:21
Show Gist options
  • Save dugsmith/b0cc43830b0279e94e7a to your computer and use it in GitHub Desktop.
Save dugsmith/b0cc43830b0279e94e7a to your computer and use it in GitHub Desktop.
Rails 4.1.x ActiveRecord serialize as JSON failing test
gem 'activerecord', '4.1.1'
require 'active_record'
require 'minitest/autorun'
require 'logger'
# Ensure backward compatibility with Minitest 4
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string "comment"
end
create_table :comments do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
serialize :comment, JSON
end
class Comment
include ActiveModel::Model
attr_accessor :category, :text
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
post.comment = Comment.new(category: "test", text: "Test is a test")
post.save!
assert post.comment.is_a?(Hash), "This is a hash in 4.0.x, but not in 4.1.x"
assert_equal "test", post.comment["category"]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment