hashie/hashie · error · Hashie::Clash::ChainError

Tried to chain into a non-hash key.

Error message

Tried to chain into a non-hash key.

What it means

Error "Tried to chain into a non-hash key." thrown in hashie/hashie.

Source

Thrown at lib/hashie/clash.rb:78

      end

      self[key.to_sym] = val
      self
    end

    def method_missing(name, *args) #:nodoc:
      if args.empty? && name.to_s.end_with?('!')
        key = name[0...-1].to_sym

        case self[key]
        when NilClass
          self[key] = self.class.new({}, self)
        when Clash
          self[key]
        when Hash
          self[key] = self.class.new(self[key], self)
        else
          raise ChainError, 'Tried to chain into a non-hash key.'
        end
      elsif args.any?
        merge_store(name, *args)
      else
        super
      end
    end

    def respond_to_missing?(method_name, _include_private = false)
      method_name = method_name.to_s

      if method_name.end_with?('!')
        key = method_name[0...-1].to_sym
        [NilClass, Clash, Hash].include?(self[key].class)
      else
        true
      end
    end

View on GitHub (pinned to fde8e03a0a)

Solutions

  1. Only chain with bang notation (key!) into keys whose current value is nil, a Hash, or a Clash; check the existing value before diving into a sub-hash.
  2. Assign a plain value with non-bang chaining (c.foo(1)) instead of c.foo! when the key should hold a scalar.
  3. If the key already holds a scalar you need to nest under, overwrite it first or restructure the chain so the sub-hash is built before the scalar is set.

Example fix

c = Hashie::Clash.new; c.order(:created_at); c.conditions!.foo('bar')._end! # only chain with bang methods into keys that are unset or hold hashes; overwrite the scalar first or use a different key.

When it happens

Trigger: Calling a bang method (e.g. clash.foo!) to chain into a key whose current value is a scalar (String, Integer, Array, etc.) instead of nil, a Hash, or a Clash.

Common situations: Building a nested options hash with Hashie::Clash and chaining into a key that was already assigned a non-hash value earlier in the chain.


AI-assisted analysis of hashie/hashie@fde8e03a0a (2026-08-23). Data as JSON: /api/errors/b736dce58911a4bd. Report an issue: GitHub.