Skip to content

Instantly share code, notes, and snippets.

@myitcv
Last active December 21, 2015 11:39
Show Gist options
  • Save myitcv/6300365 to your computer and use it in GitHub Desktop.
Save myitcv/6300365 to your computer and use it in GitHub Desktop.
Rails 4.0.0 ActiveRecord bug for serialize attribute
# 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
@senny
Copy link

senny commented Aug 22, 2013

for the record, this is the output:

-- create_table(:parents)
D, [2013-08-22T21:19:46.446683 #10786] DEBUG -- :    (0.3ms)  CREATE TABLE "parents" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255))
   -> 0.0043s
-- create_table(:children)
D, [2013-08-22T21:19:46.447206 #10786] DEBUG -- :    (0.1ms)  CREATE TABLE "children" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "parent_id" integer, "name" varchar(255), "problem" varchar(255))
   -> 0.0004s
MiniTest::Unit::TestCase is now Minitest::Test. From test.rb:30:in `<main>'
Run options: --seed 1271

# Running:

D, [2013-08-22T21:19:46.496994 #10786] DEBUG -- :    (0.0ms)  begin transaction
D, [2013-08-22T21:19:46.500706 #10786] DEBUG -- :   SQL (0.1ms)  INSERT INTO "parents" DEFAULT VALUES
D, [2013-08-22T21:19:46.500942 #10786] DEBUG -- :    (0.0ms)  commit transaction
D, [2013-08-22T21:19:46.511086 #10786] DEBUG -- :    (1.5ms)  SELECT COUNT(*) FROM "children"  WHERE "children"."parent_id" = ?  [["parent_id", 1]]
D, [2013-08-22T21:19:46.511325 #10786] DEBUG -- :    (0.1ms)  begin transaction
D, [2013-08-22T21:19:46.517850 #10786] DEBUG -- :   SQL (0.1ms)  INSERT INTO "children" ("parent_id", "problem") VALUES (?, ?)  [["parent_id", 1], ["problem", "--- {}\n"]]
D, [2013-08-22T21:19:46.518186 #10786] DEBUG -- :    (0.0ms)  commit transaction
D, [2013-08-22T21:19:46.518339 #10786] DEBUG -- :    (0.0ms)  begin transaction
D, [2013-08-22T21:19:46.519012 #10786] DEBUG -- :   SQL (0.1ms)  UPDATE "children" SET "name" = ?, "problem" = ? WHERE "children"."id" = 1  [["name", "Paul"], ["problem", "--- {}\n"]]
D, [2013-08-22T21:19:46.519126 #10786] DEBUG -- :    (0.0ms)  commit transaction
F

Finished in 0.034971s, 28.5951 runs/s, 257.3561 assertions/s.

  1) Failure:
BugTest#test_serialize_stuff [test.rb:52]:
Child.problem hash constitution has changed - Child object should report changed? == true

1 runs, 9 assertions, 1 failures, 0 errors, 0 skips

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment