Last active
October 28, 2016 11:38
-
-
Save sergey-alekseev/946657ebdb5e58d1bee115714056ec96 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
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" | |
gem "rails", github: "rails/rails" | |
gem "sqlite3" | |
end | |
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| | |
end | |
create_table :events, force: true do |t| | |
end | |
create_table :comments, force: true do |t| | |
t.integer :commentable_id | |
t.string :commentable_type | |
t.string :slug | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments, as: :commentable | |
end | |
class Event < ActiveRecord::Base | |
has_many :comments, as: :commentable | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :commentable, polymorphic: true | |
validates :slug, uniqueness: { scope: [:commentable] } | |
end | |
class BugTest < Minitest::Test | |
def test_uniqueness_validation_scope_with_polymorphic_association | |
post = Post.create! | |
post.comments << Comment.create! | |
event = Event.create! | |
event.comments << Comment.create! | |
assert_equal 1, post.comments.count | |
assert_equal 1, event.comments.count | |
end | |
end |
You can run this file using ruby rails-uniqueness-validation-scope-with-polymorphic-association_active_record_master.rb
as suggested on http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#create-an-executable-test-case.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I did not find any mention of the reported bug except this one mentioned on April 2010.