Skip to content

Instantly share code, notes, and snippets.

@mikecmpbll
Created October 17, 2013 12:09
Show Gist options
  • Save mikecmpbll/7023771 to your computer and use it in GitHub Desktop.
Save mikecmpbll/7023771 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'
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 :categories do |t|
end
create_table :categories_posts, :id => false do |t|
t.integer :category_id, :null => false
t.integer :post_id, :null => false
end
end
class Post < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Category < ActiveRecord::Base
has_and_belongs_to_many :posts
end
class TestIt < Minitest::Test
def test_it
p = Post.create
c1 = Category.create
c2 = Category.create
p.categories << c1
p.categories << c2
posts = Post.joins(:categories).where("categories_posts.category_id != ?", 1)
assert_equal [], posts
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment