Skip to content

Instantly share code, notes, and snippets.

@dpogue
Created August 18, 2017 17:35
Show Gist options
  • Save dpogue/f168bc6bc24848396bf6694623532b3a to your computer and use it in GitHub Desktop.
Save dpogue/f168bc6bc24848396bf6694623532b3a to your computer and use it in GitHub Desktop.
Standalone example showing ActiveRecord generating invalid SQL statements when trying to generate a cache key for a collection that involves joining.
# frozen_string_literal: true
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, force: true do |t|
t.timestamps
end
create_table :tags, force: true do |t|
t.timestamps
end
create_table :taggables, force: true do |t|
t.integer :tag_id
t.integer :item_id
t.string :item_type
t.timestamps
end
end
class Taggable < ActiveRecord::Base
belongs_to :tag
belongs_to :item, :polymorphic => true
end
class Post < ActiveRecord::Base
has_many :taggables, :as => :item
has_many :tags, :through => :taggables
end
class Tag < ActiveRecord::Base
has_many :taggables
end
class BugTest < Minitest::Test
def test_association_stuff
tag = Tag.create!
post = Post.create!
post.tags = [tag]
post.save
q = Post.includes(:taggables => :tag).where(:tags => {:id => tag.id})
## NOTE: Uncommenting this line causes the test to pass
#q.load
key = q.cache_key
assert_match /posts\/query-/, key
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment