Last active
August 29, 2015 14:16
-
-
Save bquorning/460ad070893cbe498df5 to your computer and use it in GitHub Desktop.
Uniqueness validation bug in Rails 4.1.10.rc1
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
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', '4.1.10.rc1' | |
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, force: true do |t| | |
t.string :title | |
t.integer :author_id | |
end | |
end | |
class Post < ActiveRecord::Base | |
self.table_name = :posts | |
validates :title, uniqueness: { scope: :author_id } | |
end | |
class BugTest < Minitest::Test | |
def test_uniqueness_with_scope_and_value | |
post = Post.new(title: 'test_1', author_id: 42) | |
assert post.save, "Should save post as unique" | |
post_2 = Post.new(title: 'test_1', author_id: 42) | |
assert !post_2.valid?, "Shouldn't be valid" | |
assert !post_2.save, "Shouldn't save post_2 as unique" | |
end | |
def test_uniqueness_with_scope_and_nil | |
post = Post.new(title: 'test_2', author_id: nil) | |
assert post.save, "Should save post as unique" | |
post_2 = Post.new(title: 'test_2', author_id: nil) | |
assert !post_2.valid?, "Shouldn't be valid" | |
assert !post_2.save, "Shouldn't save post_2 as unique" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment