Skip to content

Instantly share code, notes, and snippets.

@stas
Created July 20, 2018 19:03
Show Gist options
  • Save stas/7a9a6cbf76ffe8330059dfda0b5fa57a to your computer and use it in GitHub Desktop.
Save stas/7a9a6cbf76ffe8330059dfda0b5fa57a to your computer and use it in GitHub Desktop.
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "activerecord", "~> 4"
gem "pg", "0.19"
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(ENV['DATABASE_URL'])
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :books, force: true do |t|
t.string :title
end
create_table :tags, force: true do |t|
t.string :title
end
create_table :taggings, force: true do |t|
t.references :taggable, polymorphic: true, type: :string
t.references :tag
end
end
class Tagging < ActiveRecord::Base
belongs_to :tag
belongs_to :taggable, polymorphic: true
end
class Book < ActiveRecord::Base
has_many :taggings, as: :taggable
end
class Tag < ActiveRecord::Base
has_many :taggings
has_many :books, through: :taggings, source: :taggable, source_type: Book.name
end
class BugTest < Minitest::Test
def test_association_stuff
book = Book.create!(title: 'Book')
tag = Tag.create!(title: 'Tag')
tagging = Tagging.create!(taggable: book, tag: tag)
assert_equal 1, tag.taggings.count
assert_equal 1, tag.books.count
end
end
@stas
Copy link
Author

stas commented Jul 20, 2018

The output:

$ ruby bug.rb 
Fetching gem metadata from https://rubygems.org/...............
Resolving dependencies...
Using minitest 5.11.3
Using thread_safe 0.3.6
Using builder 3.2.3
Using tzinfo 1.2.5
Using bundler 1.16.1
Using pg 0.19.0
Using arel 6.0.4 (was 6.0.3)
Using concurrent-ruby 1.0.5 (was 1.0.2)
Using i18n 0.9.5
Using activesupport 4.2.10 (was 4.2.7.1)
Using activemodel 4.2.10 (was 4.2.7.1)
Using activerecord 4.2.10 (was 4.2.7.1)
-- create_table(:books, {:force=>true})
D, [2018-07-20T19:02:47.715125 #2821] DEBUG -- :    (10.7ms)  DROP TABLE "books"
D, [2018-07-20T19:02:47.735438 #2821] DEBUG -- :    (20.0ms)  CREATE TABLE "books" ("id" serial primary key, "title" character varying) 
   -> 0.0448s
-- create_table(:tags, {:force=>true})
D, [2018-07-20T19:02:47.742554 #2821] DEBUG -- :    (5.4ms)  DROP TABLE "tags"
D, [2018-07-20T19:02:47.762231 #2821] DEBUG -- :    (19.4ms)  CREATE TABLE "tags" ("id" serial primary key, "title" character varying) 
   -> 0.0266s
-- create_table(:taggings, {:force=>true})
D, [2018-07-20T19:02:47.768705 #2821] DEBUG -- :    (4.7ms)  DROP TABLE "taggings"
D, [2018-07-20T19:02:47.787754 #2821] DEBUG -- :    (18.7ms)  CREATE TABLE "taggings" ("id" serial primary key, "taggable_id" character varying, "taggable_type" character varying, "tag_id" integer) 
   -> 0.0254s
Run options: --seed 34828

# Running:

D, [2018-07-20T19:02:47.829625 #2821] DEBUG -- :    (0.3ms)  BEGIN
D, [2018-07-20T19:02:47.834260 #2821] DEBUG -- :   SQL (0.8ms)  INSERT INTO "books" ("title") VALUES ($1) RETURNING "id"  [["title", "Book"]]
D, [2018-07-20T19:02:47.838165 #2821] DEBUG -- :    (3.2ms)  COMMIT
D, [2018-07-20T19:02:47.842496 #2821] DEBUG -- :    (0.2ms)  BEGIN
D, [2018-07-20T19:02:47.843823 #2821] DEBUG -- :   SQL (0.3ms)  INSERT INTO "tags" ("title") VALUES ($1) RETURNING "id"  [["title", "Tag"]]
D, [2018-07-20T19:02:47.847384 #2821] DEBUG -- :    (3.1ms)  COMMIT
D, [2018-07-20T19:02:47.860303 #2821] DEBUG -- :    (0.4ms)  BEGIN
D, [2018-07-20T19:02:47.863358 #2821] DEBUG -- :   SQL (0.6ms)  INSERT INTO "taggings" ("taggable_id", "taggable_type", "tag_id") VALUES ($1, $2, $3) RETURNING "id"  [["taggable_id", "1"], ["taggable_type", "Book"], ["tag_id", 1]]
D, [2018-07-20T19:02:47.866894 #2821] DEBUG -- :    (3.0ms)  COMMIT
D, [2018-07-20T19:02:47.877946 #2821] DEBUG -- :    (0.4ms)  SELECT COUNT(*) FROM "taggings" WHERE "taggings"."tag_id" = $1  [["tag_id", 1]]
E

Finished in 0.072605s, 13.7731 runs/s, 13.7731 assertions/s.

  1) Error:
BugTest#test_association_stuff:
ActiveRecord::StatementInvalid: PG::UndefinedFunction: ERROR:  operator does not exist: integer = character varying
LINE 1: ...ROM "books" INNER JOIN "taggings" ON "books"."id" = "tagging...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(*) FROM "books" INNER JOIN "taggings" ON "books"."id" = "taggings"."taggable_id" WHERE "taggings"."tag_id" = $1 AND "taggings"."taggable_type" = $2
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/postgresql_adapter.rb:637:in `prepare'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/postgresql_adapter.rb:637:in `prepare_statement'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/postgresql_adapter.rb:596:in `exec_cache'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in `execute_and_clear'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:356:in `select'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/relation/calculations.rb:270:in `execute_simple_calculation'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/relation/calculations.rb:227:in `perform_calculation'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/relation/calculations.rb:133:in `calculate'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/relation/calculations.rb:48:in `count'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/associations/collection_association.rb:254:in `count'
    /usr/local/bundle/gems/activerecord-4.2.10/lib/active_record/associations/collection_proxy.rb:699:in `count'
    bug.rb:55:in `test_association_stuff'

1 runs, 1 assertions, 0 failures, 1 errors, 0 skips

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