Skip to content

Instantly share code, notes, and snippets.

@fractaledmind
Created December 13, 2018 18:42
Show Gist options
  • Save fractaledmind/693df573d4d5644ccd71801272bf6af6 to your computer and use it in GitHub Desktop.
Save fractaledmind/693df573d4d5644ccd71801272bf6af6 to your computer and use it in GitHub Desktop.
A test script to demonstrate a bug in ActiveRecord with finding binary data using ARel
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.2.0"
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 :binaries, force: true do |t|
t.binary :data
end
end
class Binary < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_finding_binary_data_with_arel_table_method
Binary.create(data: 'some binary data')
query_value = Binary.first.data
relation = Binary.where(data: query_value)
assert_equal relation.to_a, Binary.where(Binary.arel_table[:data].eq(query_value)).to_a
end
def test_finding_binary_data_with_arel_table_initializer
Binary.create(data: 'some binary data')
query_value = Binary.first.data
relation = Binary.where(data: query_value)
assert_equal relation.to_a, Binary.where(Arel::Table.new(Binary.table_name)[:data].eq(query_value)).to_a
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment