-
Notifications
You must be signed in to change notification settings - Fork 21.9k
Closed
Labels
Description
I am trying to merge 2 has_many
STI associations for the same table. It looks like the only way this will work is if I set create_with(nil)
for both associations before merging with or
.
Steps to reproduce
# frozen_string_literal: true
begin
require 'bundler/inline'
rescue LoadError => e
warn 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem 'rails', '6.0.3.2'
gem 'sqlite3'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# 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 :invoices, force: true do |t|
t.string :name
end
create_table :receivables, force: true do |t|
t.string :type
t.integer :invoice_id
end
end
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
class Receivable < ApplicationRecord
belongs_to :invoice
end
class Payment < Receivable
end
class AppliedCredit < Receivable
end
class Invoice < ApplicationRecord
has_many :payments, class_name: ::Payment.to_s, foreign_key: :invoice_id
has_many :applied_credits, class_name: ::AppliedCredit.to_s, foreign_key: :invoice_id
def all_payments
payments.or(applied_credits)
end
end
class BugTest < Minitest::Test
def test_association_stuff
invoice = Invoice.create!
payment = Payment.create!(invoice: invoice)
applied_credit = AppliedCredit.create!(invoice: invoice)
assert_equal invoice.all_payments.pluck(:id), [payment.id, applied_credit.id]
end
end
Expected behavior
invoice.all_payments
should return all receivables of type Payment
and AppliedCredit
Actual behavior
ArgumentError (Relation passed to #or must be structurally compatible. Incompatible values: [:create_with])
System configuration
Rails version: 6.0.3.2
Ruby version: 2.6.6