Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sergey-alekseev/946657ebdb5e58d1bee115714056ec96 to your computer and use it in GitHub Desktop.
Save sergey-alekseev/946657ebdb5e58d1bee115714056ec96 to your computer and use it in GitHub Desktop.
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
@sergey-alekseev
Copy link
Author

I did not find any mention of the reported bug except this one mentioned on April 2010.

2016-10-21-185937_1178x424_scrot

@sergey-alekseev
Copy link
Author

sergey-alekseev commented Oct 22, 2016

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.
2016-10-22-140244_634x390_scrot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment