Skip to content

Instantly share code, notes, and snippets.

@maxim
Last active August 29, 2015 14:03
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 maxim/71dbf787f6260bd819dc to your computer and use it in GitHub Desktop.
Save maxim/71dbf787f6260bd819dc to your computer and use it in GitHub Desktop.
Rails bug
unless File.exist?('Gemfile')
File.write('Gemfile', <<-GEMFILE)
source 'https://rubygems.org'
gem 'rack', github: 'rack/rack'
gem 'rails', github: 'rails/rails'
gem 'arel', github: 'rails/arel'
gem 'sqlite3'
GEMFILE
system 'bundle'
end
require 'bundler'
Bundler.setup(:default)
require 'active_record'
require 'minitest/autorun'
require 'logger'
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :seeds do |t|
t.string :seed_type
end
create_table :plants do |t|
t.string :seed_type
end
create_table :flowers do |t|
t.integer :plant_id
t.string :color
end
end
class Seed < ActiveRecord::Base
has_many :plants, -> seed { where :seed_type, seed.seed_type }
has_many :flowers, through: :plants
end
class Plant < ActiveRecord::Base
has_many :flowers
end
class Flower < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_hmt_with_conditions
poppy_seed = Seed.create!(seed_type: 'Poppy')
poppy_plant = Plant.create!(seed_type: 'Poppy')
poppy_plant.flowers.create!(color: 'red')
poppy_seed.reload
poppy_seed.flowers
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment