Skip to content

Instantly share code, notes, and snippets.

@groman-me
Forked from chipairon/gist:aaba5b07cc28905afc62
Last active August 29, 2015 14:06
Show Gist options
  • Save groman-me/f283f97c2150dc429956 to your computer and use it in GitHub Desktop.
Save groman-me/f283f97c2150dc429956 to your computer and use it in GitHub Desktop.
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'rack', github: 'rack/rack'
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 :companies do |t|
end
create_table :users do |t|
t.belongs_to :company
end
create_table :comments do |t|
t.belongs_to :post
t.belongs_to :user
end
end
class User < ActiveRecord::Base
belongs_to :company
end
class Company < ActiveRecord::Base
has_many :users
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post
belongs_to :user
end
class BugTest < Minitest::Test
def test_association_stuff
post = Post.create!
company = Company.create!
user = User.create!
post.comments << Comment.create!(user: user)
company.users << user
assert_equal 1, Post.joins(:comments).where(comments: { user: company.users }).all.size
# Passing the array works as expected:
#assert_equal 1, Post.joins(:comments).where(comments: { user: company.users.to_a }).all.size
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment