Skip to content

Polymorphic has_many with a source_type raise an exception in ActiveHash 4.0 #334

@jpcody

Description

@jpcody

The following association:

has_many :asset_types, through: :assignments, source: :assignable, source_type: "AssetType"

raises the following exception as a result of #329:

/Users/jpcody/.local/share/mise/installs/ruby/3.3.8/lib/ruby/gems/3.3.0/gems/activerecord-7.1.5.1/lib/active_record/reflection.rb:464:in `compute_class': Polymorphic associations do not support computing the class. (ArgumentError)

          raise ArgumentError, "Polymorphic associations do not support computing the class."
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        from /Users/jpcody/.local/share/mise/installs/ruby/3.3.8/lib/ruby/gems/3.3.0/gems/activerecord-7.1.5.1/lib/active_record/reflection.rb:412:in `klass'
        from /Users/jpcody/.local/share/mise/installs/ruby/3.3.8/lib/ruby/gems/3.3.0/gems/active_hash-4.0.0/lib/associations/associations.rb:14:in `has_many'
        from /Users/jpcody/Projects/CampusOptics/Code/optics-web/app/models/user.rb:38:in `<class:User>'

In our codebase. We follow this association pattern in multiple places, and while I tried to dive in a bit, we're also on the verge of upgrading Rails. For now, I'm going to monkeypatch AH to the old version and see if a newer version of Rails resolves this. But I wanted to go ahead and document the issue in case others run into it. Feel free to close it out if you don't want to have an open issue lying around without a repro case :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions