Created
March 24, 2014 16:46
-
-
Save jshirley/9744173 to your computer and use it in GitHub Desktop.
Failing example with ActiveRecord 4.1.0rc1
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
# Activate the gem you are reporting the issue against. | |
gem 'activerecord', '4.1.0rc1' | |
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 do |t| | |
end | |
create_table :categories do |t| | |
end | |
create_table :post_categories do |t| | |
t.integer :post_id | |
t.integer :category_id | |
t.boolean :prime | |
end | |
end | |
class Post < ActiveRecord::Base | |
has_many :post_categories | |
has_many :categories, through: :post_categories | |
has_many :prime_categories, | |
-> { | |
#includes(:post_category) | |
#.references(:post_category) | |
where(post_categories: { prime: true }) | |
.select("post_categories.id, categories.*") | |
}, | |
through: :post_categories, | |
source: :category | |
end | |
class Category < ActiveRecord::Base | |
has_many :post_categories | |
has_many :posts, through: :post_categories | |
end | |
class PostCategory < ActiveRecord::Base | |
belongs_to :post | |
belongs_to :category | |
end | |
class BugTest < Minitest::Test | |
def test_association_stuff | |
post = Post.create! | |
category1 = Category.create! | |
category2 = Category.create! | |
post.post_categories.create!( | |
category: category1, | |
prime: true | |
) | |
post.post_categories.create!( | |
category: category2, | |
prime: false | |
) | |
assert_equal 2, post.categories.count | |
assert_equal 1, post.prime_categories.count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment