Skip to content

Instantly share code, notes, and snippets.

@why-el
Created October 19, 2014 17:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save why-el/5010105c9c2b74a3d1db to your computer and use it in GitHub Desktop.
Reproduction script for Rails issue 17325
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'devise', github: 'plataformatec/devise'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
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 :posts do |t|
t.string :title
end
create_table :comments do |t|
t.string :content
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_one :comment
end
class Comment < ActiveRecord::Base
validates_presence_of :post_id
belongs_to :post
end
class BuildOneAssociationTest < ActiveSupport::TestCase
def test_build_should_not_trigger_save
post = Post.create!
post.comment = Comment.create!({post_id: post.id})
# This is going to fail because it is trying to save the original comment
# whose foreign_key was already sit to nil, making it invalid per validations.
post.build_comment
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment