Created
November 18, 2016 18:42
-
-
Save praveenkumarsinha/a0e77684ba12e69f6c02cff2f2197d03 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| 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 | |
| t.timestamps | |
| end | |
| create_table :appointments, force: true do |t| | |
| t.references :event | |
| t.string :name | |
| t.timestamps | |
| end | |
| create_table :transaction_charges, force: true do |t| | |
| t.references :appointment | |
| t.boolean :status | |
| t.string :name | |
| t.timestamps | |
| end | |
| create_table :seats, force: true do |t| | |
| t.references :appointment | |
| t.string :name | |
| t.timestamps | |
| 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 | |
| accepts_nested_attributes_for :transaction_charge | |
| 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_association_stuff | |
| event = Event.create(name: 'Test event') | |
| a = event.appointments.create(name: 'appointment 01', transaction_charge_attributes: {status: true, name: 'transaction_charge 01'}) | |
| a.seats.create(name: 'A1') | |
| a.seats.create(name: 'A2') | |
| a.seats.create(name: 'B1') | |
| a.seats.create(name: 'B2') | |
| a.seats.create(name: 'C9') | |
| assert_equal 1, event.appointments.count | |
| assert_equal 5, event.seats.count | |
| assert_raises(ActiveRecord::StatementInvalid) { | |
| event.synonym_seats.count | |
| } | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment