Skip to content

Instantly share code, notes, and snippets.

@utilum
Created November 19, 2016 21:59
Show Gist options
  • Save utilum/1bed25de699bf778933263aaf4ae22e6 to your computer and use it in GitHub Desktop.
Save utilum/1bed25de699bf778933263aaf4ae22e6 to your computer and use it in GitHub Desktop.
Executable test for #27069
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
# Activate the gem you are reporting the issue against.
gem "activerecord", "5.0.0.1"
gem "sqlite3"
end
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 :events, force: true do |t|
t.string :name
end
create_table :appointments, force: true do |t|
t.references :event
t.string :name
end
create_table :transaction_charges, force: true do |t|
t.references :appointment
t.boolean :status
t.string :name
end
create_table :seats, force: true do |t|
t.references :appointment
t.string :name
end
end
class Event < ActiveRecord::Base
has_many :appointments, inverse_of: :event
has_many :seats, -> { joins(' INNER JOIN `transaction_charges` ON `transaction_charges`.`appointment_id` = `appointments`.`id` ') }, through: :appointments
has_many :synonym_seats, through: :appointments, source: :seats
end
class Appointment < ActiveRecord::Base
has_one :transaction_charge, inverse_of: :appointment
has_many :seats, inverse_of: :appointment
belongs_to :event, inverse_of: :appointments
default_scope { joins(:transaction_charge).where(['transaction_charges.status =? ', true]) }
end
class TransactionCharge < ActiveRecord::Base
belongs_to :appointment, inverse_of: :transaction_charge
end
class Seat < ActiveRecord::Base
belongs_to :appointment, inverse_of: :seats
end
class BugTest < Minitest::Test
def test_respects_joins_in_default_scope_using_has_many_through
event = Event.create(name: 'Test event')
a = event.appointments.create(name: 'appointment 01')
ta = TransactionCharge.create(appointment: a, status: true, name: 'transaction_charge 01')
a.seats.create(name: 'A1')
assert_equal 1, event.appointments.count
assert_equal 1, event.seats.count
assert_match /INNER JOIN "seats"/, Event.first.synonym_seats.to_sql
assert_equal 1, event.synonym_seats.count
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment