Created
January 8, 2015 22:50
-
-
Save edpaget/d683a86fc3f7e9a48748 to your computer and use it in GitHub Desktop.
Rails 4.2 Subquery Bug
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unless File.exist?('Gemfile') | |
File.write('Gemfile', <<-GEMFILE) | |
source 'https://rubygems.org' | |
gem 'rails', github: 'rails/rails' | |
gem 'arel', github: 'rails/arel' | |
gem 'sqlite3' | |
GEMFILE | |
system 'bundle' | |
end | |
require 'bundler' | |
Bundler.setup(:default) | |
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 do |t| | |
end | |
create_table :authors do |t| | |
end | |
create_table :authors_writings do |t| | |
t.integer :author_id | |
t.integer :writing_id | |
t.string :writing_type | |
end | |
create_table :comments do |t| | |
t.integer :post_id | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
has_many :authors_writings, as: :writing | |
end | |
class Comment < ActiveRecord::Base | |
belongs_to :post | |
has_many :authors_writings, as: :writing | |
end | |
class Author < ActiveRecord::Base | |
has_many :writings | |
end | |
class AuthorsWriting < ActiveRecord::Base | |
belongs_to :author | |
belongs_to :writing, polymorphic: true | |
end | |
class BugTest < ActiveSupport::TestCase | |
attr_reader :author, :posts, :authors_writings | |
setup do | |
@author = Author.create! | |
@posts = Post.joins(:authors_writings).where(authors_writings: { author: author } ) | |
@authors_writings = AuthorsWriting.where(writing: posts) | |
end | |
test "produces a correct post query" do | |
assert_match(/\"writing_type\" = 'Post'/, posts.to_sql) | |
end | |
test "produces a correct subquery" do | |
assert_match(/IN \(SELECT.*\"writing_type\" = 'Post'/, authors_writings.to_sql) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment