Skip to content

Instantly share code, notes, and snippets.

@vbalazs
Created October 7, 2016 08:27
Show Gist options
  • Save vbalazs/a7ef6baba40b1bada7e3e431a4b57166 to your computer and use it in GitHub Desktop.
Save vbalazs/a7ef6baba40b1bada7e3e431a4b57166 to your computer and use it in GitHub Desktop.
ActiveRecord 4 save generates incorrect INSERT statement after deserialization
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.7.1"
# gem "activerecord", "5.0.0" # Fixed in 5.0 :)
gem "sqlite3"
end
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, force: true do |t|
t.text :mapped_data
end
create_table :comments, force: true do |t|
t.string :title, null: false
t.timestamps null: false
end
end
class Post < ActiveRecord::Base
serialize :mapped_data, Hash
end
class Comment < ActiveRecord::Base
validates :title, presence: true
end
class BugTest < Minitest::Test
def test_association_stuff
hash = {
foo: "bar",
animal: 3,
comment: Comment.new(title: "yaay")
}
post = Post.create!(mapped_data: hash)
assert_equal 1, Post.count
comment = post.reload.mapped_data[:comment]
assert_equal "yaay", comment.title
assert comment.valid?
comment.save! # fails with SQLite3::ConstraintException
assert_equal 1, Comment.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment