Method: Sunspot::DSL::Scope#with

Defined in:
lib/sunspot/dsl/scope.rb

#with(field_name, value = NONE) ⇒ Object

Build a positive restriction. With one argument, this method returns another DSL object which presents methods for attaching various restriction types. With two arguments, this creates a shorthand restriction: if the second argument is a scalar, an equality restriction is created; if it is a Range, a between restriction will be created; and if it is an Array, an any_of restriction will be created.

Parameters

field_name<Symbol>

Name of the field on which to place the restriction

value<Object,Range,Array>

If passed, creates an equality, range, or any-of restriction based on the type of value passed.

Returns

Sunspot::DSL::Query::Restriction

Restriction DSL object (if only one argument is passed)

Examples

An equality restriction:

Sunspot.search do
  with(:blog_id, 1)
end

Restrict by range:

Sunspot.search do
  with(:average_rating, 3.0..5.0)
end

Restrict by a set of allowed values:

Sunspot.search do
  with(:category_ids, [1, 5, 9])
end

Other restriction types:

Sunspot.search(Post) do
  with(:average_rating).greater_than(3.0)
end


63
64
65
66
67
68
69
# File 'lib/sunspot/dsl/scope.rb', line 63

def with(field_name, value = NONE)
  if value == NONE
    DSL::Restriction.new(field_name.to_sym, @query, false)
  else
    @query.add_shorthand_restriction(field_name, value)
  end
end