Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glaucocustodio/b7d643b401e37ddd2045 to your computer and use it in GitHub Desktop.
Save glaucocustodio/b7d643b401e37ddd2045 to your computer and use it in GitHub Desktop.
Scenario from issue with the use of postgres's unnest query in array columns on rails 4.2
# Activate the gem you are reporting the issue against.
# gem 'activerecord', '4.1.9' # test passing
gem 'activerecord', '4.2.0' # test failing
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.
con = ActiveRecord::Base.establish_connection(
:adapter => "postgresql",
:host => "localhost",
:username => "postgres",
:password => "123",
:database => "rails_test"
)
ActiveRecord::Base.logger = Logger.new(STDOUT)
con.connection.execute("DROP TABLE IF EXISTS logs;");
ActiveRecord::Schema.define do
create_table :logs do |t|
t.string :name
t.string :tags, array: true, default: []
t.index :tags, using: 'gin'
end
end
class Log < ActiveRecord::Base
def self.all_tags
select('unnest(tags) as id').map(&:id).uniq
end
end
Tags1 = ['bar', 'lol', 'foo']
Tags2 = ['car', 'plane']
Log.create(name: 'test', tags: Tags1)
Log.create(name: 'number 2', tags: Tags2)
puts Log.first.tags
puts Log.last.tags
puts Log.all_tags
class LogTest < Minitest::Test
def test_unnset
tags = Tags1 + Tags2
assert_equal tags, Log.all_tags
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment