Skip to content

Instantly share code, notes, and snippets.

@doits
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save doits/60b862bb45855b506ad9 to your computer and use it in GitHub Desktop.
Save doits/60b862bb45855b506ad9 to your computer and use it in GitHub Desktop.
# Activate the gem you are reporting the issue against.
gem 'activerecord', '4.1.1'
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 :people do |t|
end
create_table :houses do |t|
t.references :person
end
create_table :colors do |t|
t.references :house
t.string :color
end
create_table :conditions do |t|
t.string :condition
end
create_table :conditions_houses do |t|
t.references :condition
t.references :house
end
end
class Color < ActiveRecord::Base
belongs_to :house
end
class Condition < ActiveRecord::Base
has_and_belongs_to_many :houses
end
class House < ActiveRecord::Base
has_many :colors
belongs_to :person
has_and_belongs_to_many :conditions
scope :damaged, -> { joins(:conditions).where(:'conditions.condition' => 'damaged') }
end
class Person < ActiveRecord::Base
has_many :houses
has_many :colors, :through => :houses
has_many :damaged_houses, -> { damaged }, :class_name => "House"
has_many :damaged_colors, through: :damaged_houses, :source => :colors
end
class BugTest < Minitest::Test
def test_association_stuff
Condition.create(condition: :damaged)
house = House.create(conditions: [Condition.first])
person = Person.create
house.person = person
color = Color.create
house.colors = [color]
house.save
assert_equal 1, person.houses.count
assert_equal 1, person.damaged_houses.count
assert_equal 1, person.colors.count
assert_equal 1, person.damaged_colors.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment