Skip to content

Instantly share code, notes, and snippets.

@bquorning
Last active July 4, 2016 12:06
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/46453238071c2221f200c84b54cd5e9a to your computer and use it in GitHub Desktop.
Save bquorning/46453238071c2221f200c84b54cd5e9a to your computer and use it in GitHub Desktop.
An instance of ActionController::Parameters stored in a column as YAML with Rails 4, cannot be read with 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.preferences.class)
end
end
# Output
#
# BugTest#test_serialization [store-issue-rails-4.rb:54]:
# Expected: Hash
# Actual: ActionController::Parameters
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', '5.0.0'
gem 'actionpack', '5.0.0'
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!
User.connection.execute(
"UPDATE users SET preferences = '--- !ruby/hash-with-ivars:ActionController::Parameters\nelements:\n color: red\n font: Times\nivars:\n :@permitted: \n';"
)
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.preferences.class)
end
end
# Output
#
# BugTest#test_serialization:
# NoMethodError: undefined method `[]=' for nil:NilClass
# /Users/bquorning/.gem/ruby/2.3.1/gems/actionpack-5.0.0/lib/action_controller/metal/strong_parameters.rb:414:in `[]='
# /Users/bquorning/.rubies/ruby-2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:362:in `block in revive_hash'
# /Users/bquorning/.rubies/ruby-2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:336:in `each'
# /Users/bquorning/.rubies/ruby-2.3.1/lib/ruby/2.3.0/psych/visitors/to_ruby.rb:336:in `each_slice'
# [...]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment