Skip to content

Instantly share code, notes, and snippets.

@bquorning
Last active July 4, 2016 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bquorning/ce96c6296eb66ab53d8ccb87822225da to your computer and use it in GitHub Desktop.
Save bquorning/ce96c6296eb66ab53d8ccb87822225da to your computer and use it in GitHub Desktop.
Models using `store` to serialize a `Hash` in the form of `ActionController::Parameters` in Rails 4 cannot read that same column in Rails 5.
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.2.6'
gem 'actionpack', '4.2.6'
gem 'sqlite3'
end
require 'active_record'
require 'action_controller'
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 :users, force: true do |t|
t.text :preferences
end
end
class User < ActiveRecord::Base
store :preferences, accessors: [
:color,
:font
]
end
class BugTest < Minitest::Test
def test_serialization
user = User.create!(
preferences: ActionController::Parameters.new(color: "red", font: "Times")
)
user = user.reload
assert_equal(
"--- !ruby/hash-with-ivars:ActionController::Parameters\nelements:\n color: red\n font: Times\nivars:\n :@permitted: \n",
user.read_attribute_before_type_cast(:preferences)
)
assert_equal(Hash, user.reload.preferences.class)
end
end
# Output:
#
# 1) Failure:
# BugTest#test_serialization [active_record_gem.rb:47]:
# Expected: Hash
# Actual: ActionController::Parameters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment