Last active
December 21, 2015 11:39
-
-
Save myitcv/6300365 to your computer and use it in GitHub Desktop.
Rails 4.0.0 ActiveRecord bug for serialize attribute
This file contains hidden or 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
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.0.0' | |
require 'active_record' | |
require 'minitest/autorun' | |
require 'logger' | |
# 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 :parents do |t| | |
t.string :name | |
end | |
create_table :children do |t| | |
t.references :parent | |
t.string :name | |
t.string :problem | |
end | |
end | |
class Parent < ActiveRecord::Base | |
has_many :children | |
end | |
class Child < ActiveRecord::Base | |
belongs_to :parent | |
serialize :problem, Hash | |
end | |
class BugTest < Minitest::Unit::TestCase | |
def test_serialize_stuff | |
parent = Parent.create | |
assert_nil parent.name | |
assert_equal 0, parent.children.count | |
child = parent.children.create | |
assert_nil child.name | |
assert_equal Hash.new, child.problem | |
assert !child.changed? | |
child.name = 'Paul' | |
assert child.changed? | |
child.save | |
assert !child.changed? | |
h = child.problem | |
h[:test] = 5 | |
assert_equal 5, child.problem[:test] | |
assert child.changed?, "Child.problem hash constitution has changed - Child object should report changed? == true" | |
child = parent.children.first | |
assert !child.changed? | |
child.problem = Hash.new.merge(h) | |
assert child.changed? | |
assert_equal 5, child.problem[:test] | |
child.save | |
assert !child.changed? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for the record, this is the output: