activerecord-hackery/ransack · error · ArgumentError

Don't know what context to use for #{object}

Error message

Don't know what context to use for #{object}

What it means

Ransack::Context.for is the factory used by Ransack::Search.new to build a query context. It only knows how to handle (a) classes that inherit from ActiveRecord::Base and (b) ActiveRecord::Relation instances. Anything else — a String class name, a model instance, a non-ActiveRecord object — returns nil from both for_class and for_object, and this ArgumentError is raised.

Source

Thrown at lib/ransack/context.rb:31

          Adapters::ActiveRecord::Context.new(klass, options)
        end
      end

      def for_object(object, options = {})
        case object
        when ActiveRecord::Relation
          Adapters::ActiveRecord::Context.new(object.klass, options)
        end
      end

      def for(object, options = {})
        context =
          if Class === object
            for_class(object, options)
          else
            for_object(object, options)
          end
        context or raise ArgumentError,
          "Don't know what context to use for #{object}"
      end

    end # << self

    def initialize(object, options = {})
      @object = relation_for(object)
      @klass = @object.klass
      @join_dependency = join_dependency(@object)
      @join_type = options[:join_type] || Polyamorous::OuterJoin
      @search_key = options[:search_key] || Ransack.options[:search_key]
      @associations_pot = {}
      @tables_pot = {}
      @lock_associations = []

      @base = @join_dependency.instance_variable_get(:@join_root)
    end

View on GitHub (pinned to e82f6bab39)

Solutions

  1. Pass the model class: Person.ransack(params[:q])
  2. Or pass an ActiveRecord::Relation: Person.where(active: true).ransack(params[:q])
  3. If you have a record, use record.class.ransack(...)
  4. For non-AR models, Ransack will not work — write the query manually or use an AR-backed model

Example fix

# before
@q = Ransack::Search.new('Person', params[:q])
# => ArgumentError: Don't know what context to use for Person

# after
@q = Person.ransack(params[:q])
Defensive patterns

Strategy: type-guard

Validate before calling

target = Person if target.is_a?(Person)
target = target.class if target.is_a?(ActiveRecord::Base)
target = target.klass if target.is_a?(ActiveRecord::Relation)

@q = if target.is_a?(Class) && target < ActiveRecord::Base
  target.ransack(params[:q])
else
  Person.ransack(params[:q])
end

Type guard

def ransackable_target?(obj)
  (obj.is_a?(Class) && obj < ActiveRecord::Base) || obj.is_a?(ActiveRecord::Relation)
end

Prevention

When it happens

Trigger: Ransack::Search.new('Person', params) (string name), Ransack::Search.new(person_instance, params) (single record), Person.ransack where the adapter is missing, or passing a Mongoid/other-ORM model that has no Ransack adapter.

Common situations: Porting old code that passed class names as strings (no longer supported); passing @person = Person.find(...) instead of the class or a relation; attempting to use Ransack with a non-ActiveRecord ORM; accidentally calling .ransack on an array from a plain Ruby query.

Related errors


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