-
-
Save maletor/af1aae5086ba10ed7cad to your computer and use it in GitHub Desktop.
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
require 'byebug' | |
require 'rubygems' | |
gem 'activerecord', '= 4.1.5' | |
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: 'mysql2', database: 'foo') | |
# ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:') | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
# ActiveRecord::Schema.define do | |
# create_table :address_books | |
# | |
# create_table :imported_contacts do |t| | |
# t.belongs_to :address_book | |
# t.string :email | |
# end | |
# | |
# create_table :users do |t| | |
# t.belongs_to :account | |
# end | |
# | |
# create_table :accounts do |t| | |
# t.string :login | |
# end | |
# | |
# create_table :vendor_email_addresses do |t| | |
# t.belongs_to :user | |
# t.string :email | |
# end | |
# end | |
class AddressBook < ActiveRecord::Base | |
belongs_to :user, inverse_of: :address_books | |
has_many :imported_contacts, inverse_of: :address_book | |
has_many :matched_users, through: :imported_contacts, source: :matched_user | |
has_many :matched_vendor_users, through: :imported_contacts, source: :matched_vendor_user | |
end | |
class ImportedContact < ActiveRecord::Base | |
belongs_to :address_book, inverse_of: :imported_contacts | |
has_one :user, through: :address_book | |
has_one :matched_account, class_name: "Account", foreign_key: :login, primary_key: :email | |
has_one :matched_vendor_email_address, class_name: "VendorEmailAddress", foreign_key: :email, primary_key: :email | |
has_one :matched_user, class_name: "User", through: :matched_account, source: :user | |
has_one :matched_vendor_user, class_name: "User", through: :matched_vendor_email_address, source: :user | |
end | |
class VendorEmailAddress < ActiveRecord::Base | |
belongs_to :user | |
end | |
class User < ActiveRecord::Base | |
has_many :address_books | |
belongs_to :account | |
has_many :vendor_email_addresses | |
end | |
class Account < ActiveRecord::Base | |
has_one :user | |
end | |
class BugTest < Minitest::Test | |
def test_union | |
address_book = AddressBook.create! | |
union = address_book.matched_users.union(address_book.matched_vendor_users) | |
union.to_sql | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With sqlite3
With mysql2