Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentError: Polymorphic association does not support to compute class #558

Closed
mediafinger opened this issue Jun 3, 2021 · 2 comments
Closed

Comments

@mediafinger
Copy link

Thank you for maintaining this library 馃槂
We only encountered it now, after having a composite index in one of our MySQL tables and threw it in the mix. Surprisingly this leads to multiple errors in unrelated classes:

The call to source_reflection.klass will raise the ArgumentError:
Polymorphic association does not support to compute class
when source_reflection.klass is polymorphic.

module ActiveRecord
  module Associations
    module ThroughAssociation
      alias :original_construct_join_attributes :construct_join_attributes

      def construct_join_attributes(*records)
        # CPK
        if source_reflection.klass.composite?
          ensure_mutable

          ids = records.map do |record|
            source_reflection.association_primary_key(reflection.klass).map do |key|
              record.send(key)
            end
          end

          cpk_in_predicate(through_association.scope.klass.arel_table, source_reflection.foreign_key, ids)
        else
          original_construct_join_attributes(*records)
        end
      end
    end
  end
end

This change was discussed here: rails/rails#31877
and implemented here: rails/rails@fb86ecd

For our code base a monkey-patch to change the condition to:
if !source_reflection.polymorphic? && source_reflection.klass.composite?
would suffice, but I doubt this would be a good solution for this gem.

@cfis
Copy link
Contributor

cfis commented Aug 21, 2021

I'm ok with that change. Would you be able to submit a patch with a test case?

@cfis cfis closed this as completed in 99e70dd Jan 10, 2022
@cfis
Copy link
Contributor

cfis commented Jan 10, 2022

After looking at this again, I think your patch is good enough for now and I applied it to the Rails 6.1 and Rails 7 branches. Thanks!

cfis added a commit that referenced this issue Jan 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants