Method: Sequel::Dataset#where

Defined in:
lib/sequel/dataset/query.rb

#where(*cond, &block) ⇒ Object

Returns a copy of the dataset with the given WHERE conditions imposed upon it.

Accepts the following argument types:

Hash, Array of pairs

list of equality/inclusion expressions

Symbol

taken as a boolean column argument (e.g. WHERE active)

Sequel::SQL::BooleanExpression, Sequel::LiteralString

an existing condition expression, probably created using the Sequel expression filter DSL.

where also accepts a block, which should return one of the above argument types, and is treated the same way. This block yields a virtual row object, which is easy to use to create identifiers and functions. For more details on the virtual row support, see the “Virtual Rows” guide

If both a block and regular argument are provided, they get ANDed together.

Examples:

DB[:items].where(id: 3)
# SELECT * FROM items WHERE (id = 3)

DB[:items].where(Sequel.lit('price < ?', 100))
# SELECT * FROM items WHERE price < 100

DB[:items].where([[:id, [1,2,3]], [:id, 0..10]])
# SELECT * FROM items WHERE ((id IN (1, 2, 3)) AND ((id >= 0) AND (id <= 10)))

DB[:items].where(Sequel.lit('price < 100'))
# SELECT * FROM items WHERE price < 100

DB[:items].where(:active)
# SELECT * FROM items WHERE :active

DB[:items].where{price < 100}
# SELECT * FROM items WHERE (price < 100)

Multiple where calls can be chained for scoping:

software = dataset.where(category: 'software').where{price < 100}
# SELECT * FROM items WHERE ((category = 'software') AND (price < 100))

See the “Dataset Filtering” guide for more examples and details.



1126
1127
1128
# File 'lib/sequel/dataset/query.rb', line 1126

def where(*cond, &block)
  add_filter(:where, cond, &block)
end