Created
August 26, 2016 19:05
-
-
Save duffyjp/80e1d10a6a5f329baefc3a3865e058a0 to your computer and use it in GitHub Desktop.
Rails 5.0.0/5.0.0.1 ActiveRecord::QueryMethods - references testcase
This file contains hidden or 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
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" | |
# Activate the gem you are reporting the issue against. | |
gem "activerecord", "5.0.0.1" | |
gem "sqlite3" | |
end | |
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. | |
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| | |
end | |
create_table :foo_comments, force: true do |t| | |
t.integer :post_id | |
t.string :content | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :comments | |
end | |
class Comment < ActiveRecord::Base | |
self.table_name = 'foo_comments' | |
belongs_to :post | |
end | |
class BugTest < Minitest::Test | |
def test_references | |
post = Post.create! | |
post.comments << Comment.create!(content: 'bar') | |
# references takes table_name, this should work and does. | |
assert_equal Post.includes(:comments).references(:foo_comments).where("foo_comments.content = ?", 'bar').first, post | |
# demonstrating references is required with a string conditional | |
assert_raises ActiveRecord::StatementInvalid do | |
Post.includes(:comments).where("foo_comments.content = ?", 'bar').first | |
end | |
# My personal opinion is this *should* work, but the docs say references takes table_name, so it shouldn't | |
# refute_equal Post.includes(:comments).references(:comments).where("foo_comments.content = ?", 'bar').first, post | |
# This should fail miserably, but it generates valid SQL and finds the post. | |
refute_equal Post.includes(:comments).references(:literally_anything).where("foo_comments.content = ?", 'bar').first, post | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment