{"record":{"id":"a095d5a0b78dcbe4","repo":"activerecord-hackery/ransack","slug":"value-cannot-be-converted-to-a-class","errorCode":null,"errorMessage":"#{value} cannot be converted to a Class","messagePattern":"#(.+?) cannot be converted to a Class","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/polyamorous/join.rb","lineNumber":65,"sourceCode":"      when Class\n        if [InnerJoin, OuterJoin].include? type\n          type\n        else\n          raise ArgumentError, \"#{type} cannot be converted to an ARel join type\"\n        end\n      else\n        raise ArgumentError, \"#{type} cannot be converted to an ARel join type\"\n      end\n    end\n\n    def convert_to_class(value)\n      case value\n      when String, Symbol\n        Kernel.const_get(value)\n      when Class\n        value\n      else\n        raise ArgumentError, \"#{value} cannot be converted to a Class\"\n      end\n    end\n\n  end\nend\n","sourceCodeStart":47,"sourceCodeEnd":71,"githubUrl":"https://github.com/activerecord-hackery/ransack/blob/e82f6bab3956c5597e7debbefa218d9e94e58ceb/lib/polyamorous/join.rb#L47-L71","documentation":"Polyamorous::Join#convert_to_class raises this ArgumentError when the klass argument (third argument to Polyamorous::Join.new, or the value assigned via Join#klass=) is anything other than a String, Symbol, or Class. Strings/Symbols are resolved with Kernel.const_get, so they must name a real constant. This argument is only needed for polymorphic joins (the '_of_Model_type' syntax) to pin the join to a concrete class.","triggerScenarios":"Polyamorous::Join.new(:notable, :inner, 42), Join.new(:notable, :inner, nil_via_hash_explosion), or join.klass = some_object_instance. Also raised indirectly when a malformed polymorphic association string yields a non-class value that gets forwarded as klass.","commonSituations":"Building polymorphic joins by hand and passing a model instance instead of its class; passing a Hash of options where the class is expected; a nil or integer leaking in from params or config when constructing custom join trees.","solutions":["Pass a Class (Person), a Symbol (:Person), or a String ('Person') as the klass argument","If you hold a model instance, pass instance.class instead of the instance","Check upstream data: verify polymorphic '_of_X_type' parsing in your code produces a constant name, not an arbitrary object"],"exampleFix":"# before\nPolyamorous::Join.new(:notable, :inner, article_instance)\n# => ArgumentError: #<Article:0x...> cannot be converted to a Class\n\n# after\nPolyamorous::Join.new(:notable, :inner, article_instance.class)\n# or\nPolyamorous::Join.new(:notable, :inner, 'Article')","handlingStrategy":"validation","validationCode":"klass = klass.class if klass.is_a?(ActiveRecord::Base)\nunless klass.is_a?(Class) || klass.is_a?(String) || klass.is_a?(Symbol)\n  raise ArgumentError, \"klass must be a Class/String/Symbol, got #{klass.class}\"\nend\njoin = Polyamorous::Join.new(:notable, :inner, klass)","typeGuard":"def join_klass_candidate?(value)\n  value.is_a?(Class) || value.is_a?(String) || value.is_a?(Symbol)\nend","tryCatchPattern":"begin\n  join.klass = klass_value\nrescue ArgumentError, NameError => e\n  Rails.logger.error(\"Invalid join klass #{klass_value.inspect}: #{e.message}\")\n  raise\nend","preventionTips":["Never pass model instances as the klass argument — pass instance.class","If klass comes from params (polymorphic _of_X_type), validate it against your model list before const_get","Watch for NameError from Kernel.const_get on string klasses — invalid names fail there before this check"],"tags":["ruby","rails","ransack","polyamorous","polymorphic","argumenterror"],"backgroundTag":"invalid-argument-type","analyzedSha":"e82f6bab3956c5597e7debbefa218d9e94e58ceb","analyzedAt":"2026-08-21T19:30:23.639Z","schemaVersion":2},"datasetVersion":"2026-08-21T23:17:16.201Z"}