Skip to content

Instantly share code, notes, and snippets.

@npezza93
Created January 4, 2017 03:45
Show Gist options
  • Save npezza93/3a34d8b023669c70bf5f16d89ab0d111 to your computer and use it in GitHub Desktop.
Save npezza93/3a34d8b023669c70bf5f16d89ab0d111 to your computer and use it in GitHub Desktop.
Example script showing a bug when trying to set singular_collection_ids with a primary key set on relationship
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler 1.10 or later is required. Please update Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.2.7.1'
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 :users, force: true do |t|
end
create_table :user_businesses, force: true do |t|
t.belongs_to :user
t.string :business_id, index: true
end
create_table :businesses, force: true do |t|
t.string :uuid
end
end
class User < ActiveRecord::Base
has_many :user_businesses
has_many :businesses, through: :user_businesses
end
class UserBusiness < ActiveRecord::Base
belongs_to :user
belongs_to :business, primary_key: :uuid
end
class Business < ActiveRecord::Base
end
class SingularCollectionAssociationIdsTest < Minitest::Test
def test_singular_collection_ids=
user = User.create!
business = Business.create!(uuid: SecureRandom.uuid)
exception = assert_raises ActiveRecord::RecordNotFound do
user.business_ids = [business.uuid]
end
assert_equal(
"Couldn't find Business with 'id'=#{business.uuid.to_i}",
exception.message
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment