Module: ActiveEnumerable::Where

Included in:
EnglishDsl
Defined in:
lib/active_enumerable/where.rb

Defined Under Namespace

Classes: WhereNotChain

Instance Method Summary collapse

Instance Method Details

#where(conditions = nil) ⇒ Object

Returns a new relation, which is the result of filtering the current relation according to the conditions in the arguments.

hash

#where will accept a hash condition, in which the keys are fields and the values are values to be searched for.

Fields can be symbols or strings. Values can be single values, arrays, or ranges.

<#ActiveEnumerable>.where({ name: "Joe", email: "[email protected]" })

<#ActiveEnumerable>.where({ name: ["Alice", "Bob"]})

<#ActiveEnumerable>.where({ created_at: (Time.now.midnight - 1.day)..Time.now.midnight })

<#ActiveEnumerable>.where(contracts:[{ created_at: (Time.now.midnight - 1.day)..Time.now.midnight }])

.or

Returns a new relation, which is the logical union of this relation and the one passed as an argument.

The two relations must be structurally compatible: they must be scoping the same model, and they must differ only by #where.

<#ActiveEnumerable>.where(id: 1).or(<#ActiveEnumerable>.where(author_id: 3))

Additional conditions can be passed to where in hash form.

<#ActiveEnumerable>.where(id: 1).or(author_id: 3)


61
62
63
64
65
66
# File 'lib/active_enumerable/where.rb', line 61

def where(conditions=nil)
  return WhereNotChain.new(all, method(:__new_relation__)) if conditions.nil?
  enable_or create_where_relation(conditions, to_a.select do |record|
    Finder.new(record).is_of(conditions)
  end)
end

#where_conditionsObject



90
91
92
# File 'lib/active_enumerable/where.rb', line 90

def where_conditions
  @where_conditions ||= {}
end