Module: ActiveEnumerable::Where
- Included in:
- EnglishDsl
- Defined in:
- lib/active_enumerable/where.rb,
lib/active_enumerable/where/where_or_chain.rb,
lib/active_enumerable/where/where_not_chain.rb
Defined Under Namespace
Modules: WhereOrChain Classes: WhereNotChain
Instance Method Summary collapse
-
#where(conditions = nil, &block) ⇒ Object
Returns a new relation, which is the result of filtering the current relation according to the conditions in the arguments.
- #where_conditions ⇒ Object
Instance Method Details
#where(conditions = nil, &block) ⇒ 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)
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/active_enumerable/where.rb', line 40 def where(conditions = nil, &block) return WhereNotChain.new(all, method(:__new_relation__)) unless conditions || block conditions = conditions || { nil => block } create_where_relation(conditions, to_a.select do |record| Finder.new(record).is_of(conditions || { nil => block }) end).tap do |where| where.extend(WhereOrChain) where.original_collection = to_a end end |
#where_conditions ⇒ Object
51 52 53 |
# File 'lib/active_enumerable/where.rb', line 51 def where_conditions @where_conditions ||= {} end |