Skip to content

Instantly share code, notes, and snippets.

@composerinteralia
Last active January 31, 2017 21:27
Show Gist options
  • Save composerinteralia/a551dc391258dd2bfeeb983956a37a49 to your computer and use it in GitHub Desktop.
Save composerinteralia/a551dc391258dd2bfeeb983956a37a49 to your computer and use it in GitHub Desktop.
Collection Singular Ids Bug
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 "arel", github: "rails/arel"
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 do |t|
end
create_table :taggings do |t|
t.integer :tag_id
t.integer :post_id
end
create_table :tags do |t|
end
end
class Post < ActiveRecord::Base
# works with primary_key: "id" or without specifying primary_key
has_many :taggings, primary_key: :id
has_many :tags, through: :taggings
end
class Tagging < ActiveRecord::Base
# works with primary_key: "id" or without specifying primary_key
belongs_to :tag, primary_key: :id
belongs_to :post
end
class Tag < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_collection_singular_ids_eq
tagging = Tagging.create!
post = Post.new(tagging_ids: [tagging.id.to_s])
assert post.valid?
end
def test_collection_singular_ids_eq_with_through_reflection
tag = Tag.create!
post = Post.new(tag_ids: [tag.id.to_s])
assert post.valid?
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment