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, nameView on GitHub (pinned to e82f6bab39)
Solutions
- Pass the context: Ransack::Translate.association(:articles, context: @q.context)
- Use the search-level API instead: @q.translate(:articles) or form builder labels, which supply the context for you
- 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
- Supply context: search.context for every direct Translate.association call
- Remember the association key must be traversable from the context's base class, or traverse raises separately
- Use the built-in label/i18n paths first; drop to Translate.* only when you must, and always with a context
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
- A context is required to translate attributes
- #{type} cannot be converted to an ARel join type
- #{value} cannot be converted to a Class
- Don't know how to klassify #{obj}
- Don't know what context to use for #{object}
AI-assisted analysis of activerecord-hackery/ransack@e82f6bab39 (2026-08-21).
Data as JSON: /api/errors/2f08d35b8036f5f6.
Report an issue: GitHub.