Skip to content

Instantly share code, notes, and snippets.

@senny
Created August 26, 2013 11:59
Show Gist options
  • Save senny/6340734 to your computer and use it in GitHub Desktop.
Save senny/6340734 to your computer and use it in GitHub Desktop.
unless File.exists?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails', branch: '3-2-stable'
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_archive do |t|
t.string :title
end
create_table :comments do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
self.table_name = "posts_archive"
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < Minitest::Unit::TestCase
def test_where_with_association_and_different_table_name
Comment.joins(:post).where(post: {title: "Welcome"}).all
end
end
@senny
Copy link
Author

senny commented Aug 26, 2013

-- create_table(:posts_archive)
D, [2013-08-26T13:58:20.536680 #41367] DEBUG -- :    (11.5ms)  select sqlite_version(*)
D, [2013-08-26T13:58:20.537291 #41367] DEBUG -- :    (0.3ms)  CREATE TABLE "posts_archive" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255))
   -> 0.0256s
-- create_table(:comments)
D, [2013-08-26T13:58:20.537973 #41367] DEBUG -- :    (0.1ms)  CREATE TABLE "comments" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "post_id" integer)
   -> 0.0006s
Run options: --seed 64415

# Running tests:

D, [2013-08-26T13:58:20.569371 #41367] DEBUG -- :   Comment Load (0.1ms)  SELECT "comments".* FROM "comments" INNER JOIN "posts_archive" ON "posts_archive"."id" = "comments"."post_id" WHERE "post"."title" = 'Welcome'
D, [2013-08-26T13:58:20.569456 #41367] DEBUG -- : SQLite3::SQLException: no such column: post.title: SELECT "comments".* FROM "comments" INNER JOIN "posts_archive" ON "posts_archive"."id" = "comments"."post_id" WHERE "post"."title" = 'Welcome'
E

Finished tests in 0.016296s, 61.3648 tests/s, 0.0000 assertions/s.

  1) Error:
test_where_with_association_and_different_table_name(BugTest):
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: post.title: SELECT "comments".* FROM "comments" INNER JOIN "posts_archive" ON "posts_archive"."id" = "comments"."post_id" WHERE "post"."title" = 'Welcome'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment