Last active
January 10, 2017 11:02
-
-
Save phansch/ab172e4b8cadf24f1e6508a0555e465c to your computer and use it in GitHub Desktop.
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
# Run with `ruby association_testcase.rb` | |
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 "sqlite3" | |
gem "pry" | |
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.string :title | |
end | |
create_table :comments, force: true do |t| | |
t.text :body, null: false | |
t.integer :post_id | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
validates :title, uniqueness: true | |
accepts_nested_attributes_for :comments | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
end | |
class BugTest < Minitest::Test | |
def test_nested_attributes_rollback_behaviour | |
posts = [Post.new(title: "The truth about toothpaste and toothbrushes", comments_attributes: [{ body: "Comment" }])] | |
ActiveRecord::Base.transaction do | |
posts.each do |p| | |
p.save | |
# Collecting validation errors here for presentation purposes | |
end | |
raise ActiveRecord::Rollback | |
end | |
# Now the comment attributes seem to be gone | |
# and this line should not raise | |
posts.each { |p| post.save } | |
assert_equal 1, posts.first.comments.count | |
assert_equal "Comment", Comment.first.body | |
assert_equal post.id, Comment.first.post.id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment