activerecord-hackery/ransack · error · ArgumentError

A context is required to translate associations

Error message

A context is required to translate associations

What it means

Ransack::Translate.association resolves a human name for an association key (or the model itself when key is blank) via I18n ('ransack.associations.<model>.<key>', falling back to context.traverse(key).model_name.human). Traversal needs the search context's base class, so options[:context] is mandatory; omitting it raises this ArgumentError.

Source

Thrown at lib/ransack/translate.rb:58

        interpolations = {
          attributes: translated_names.join(" #{Translate.word(combinator)} ")
        }

        if predicate
          defaults << "%{attributes} %{predicate}".freeze
          interpolations[:predicate] = Translate.predicate(predicate)
        else
          defaults << "%{attributes}".freeze
        end

        options.reverse_merge! count: 1, default: defaults
        I18n.translate(defaults.shift, **options.merge(interpolations))
      end

      def association(key, options = {})
        unless context = options.delete(:context)
          raise ArgumentError, "A context is required to translate associations"
        end

        defaults =
          if key.blank?
            [:"ransack.models.#{i18n_key(context.klass)}",
             :"#{context.klass.i18n_scope}.models.#{i18n_key(context.klass)}"]
          else
            [:"ransack.associations.#{i18n_key(context.klass)}.#{key}"]
          end
        defaults << context.traverse(key).model_name.human
        options = { count: 1, default: defaults }
        I18n.translate(defaults.shift, **options)
      end

      private

      def attribute_name(context, name, include_associations = nil)
        @context, @name = context, name

View on GitHub (pinned to e82f6bab39)

Solutions

  1. Pass the context: Ransack::Translate.association(:articles, context: @q.context)
  2. Use the search-level API instead: @q.translate(:articles) or form builder labels, which supply the context for you
  3. Ensure custom helpers forward the full options hash including :context when delegating to Translate

Example fix

# before
Ransack::Translate.association(:articles)
# => ArgumentError: A context is required to translate associations

# after
search = Person.ransack
Ransack::Translate.association(:articles, context: search.context)
Defensive patterns

Strategy: validation

Validate before calling

search = Person.ransack
Ransack::Translate.association(:articles, context: search.context)

Type guard

def translatable_association_call?(key, options)
  options.key?(:context) && options[:context].respond_to?(:traverse)
end

Prevention

When it happens

Trigger: Ransack::Translate.association(:articles) with no context option; custom view helpers that call Translate.association while forwarding an options hash that dropped :context; translating nested association labels (e.g. 'author_articles_title') outside a real search.

Common situations: Custom label/partial code extracted from Ransack forms that calls the Translate module directly; I18n-related specs; refactors where the options hash is rebuilt and :context is lost.

Related errors


AI-assisted analysis of activerecord-hackery/ransack@e82f6bab39 (2026-08-21). Data as JSON: /api/errors/2f08d35b8036f5f6. Report an issue: GitHub.