Skip to content

Instantly share code, notes, and snippets.

@mfazekas
Created December 9, 2014 14:30
Show Gist options
  • Save mfazekas/a2b030278931a209cc23 to your computer and use it in GitHub Desktop.
Save mfazekas/a2b030278931a209cc23 to your computer and use it in GitHub Desktop.
rr42_active_record pluck and bind values
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
gem 'pg'
gem 'byebug'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
require 'byebug'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'postgresql', database: 'rails_test', username:'rails')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table "bars", force: true do |t|
t.integer "object_id"
t.string "object_type"
end
create_table "foos", force: true do |t|
end
end
class Foo < ActiveRecord::Base
has_many :bars, as: :object
end
class Bar < ActiveRecord::Base
belongs_to :object, polymorphic: true
end
class PluckTest < Minitest::Test
def test_polymorphic_with_pluck
scope = Foo.joins(:bars)
foo = Foo.create()
Bar.create(object_id:foo.id,object_type:'nop')
assert_equal 0, scope.count
assert_equal [], scope.pluck(:id)
end
end
# PG::ProtocolViolation: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
# : SELECT "foos"."id" FROM "foos" INNER JOIN "bars" ON "bars"."object_id" = "foos"."id" AND "bars"."object_type" = $1
# E
# Finished in 0.026868s, 37.2190 runs/s, 37.2190 assertions/s.
# 1) Error:
# PluckTest#test_polymorphic_with_pluck:
# ActiveRecord::StatementInvalid: PG::ProtocolViolation: ERROR: bind message supplies 0 parameters, but prepared statement "" requires 1
# : SELECT "foos"."id" FROM "foos" INNER JOIN "bars" ON "bars"."object_id" = "foos"."id" AND "bars"."object_type" = $1
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters#/postgresql_adapter.rb:592:in `async_exec'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `block in exec_no_cache'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `block in log'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activesupport/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb:460:in `log'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:592:in `exec_no_cache'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb:584:in `execute_and_clear'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/postgresql/database_statements.rb:160:in `exec_query'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:336:in `select'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:32:in `select_all'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in `select_all'
# /Users/boga/.rvm/gems/ruby-2.0.0-p451/bundler/gems/rails-8e52954349c2/activerecord/lib/active_record/relation/calculations.rb:180:in `pluck'
# pg_test2.rb:51:in `test_polymorphic_with_pluck'
#
# 1 runs, 1 assertions, 0 failures, 1 errors, 0 skips
#
# shell returned 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment