Clear ORM
  • Welcome to Clear
  • Introduction
    • Setup
  • Model
    • Defining your model
      • Describing your columns
      • Primary Keys
      • Converters
    • Associations
      • belongs_to
      • has_many
      • has_many through
      • has_one
    • Lifecycle
      • Persistence
      • Validations
      • Triggers
    • Batchs operations
      • Bulk update
      • Bulk insert & delete
    • Transactions & Save Points
      • Transaction & Savepoints
      • Connection pool
    • Locks
  • Querying
    • The collection object
      • Filter the query
        • Filter the query – The Expression Engine
        • Find, First, Last, Offset, Limit
        • Aggregation
        • Ordering & Group by
      • Fetching the query
        • Each and Fetch
        • Cursored fetching
        • Model extra attributes
      • Joins
      • Eager Loading
      • Window and CTE
      • Scopes
    • Writing low-level SQL
      • Select Clause
      • Insert Clause
      • Delete Clause
  • Migrations
    • Manage migrations
    • Call migration script
    • Migration CLI
  • Additional and advanced features
    • JSONB
    • Symbol vs String
    • Enums
    • BCrypt
    • Full Text Search
    • Handling multi-connection
  • Other resources
    • API Documentation
    • Inline documentation
    • Github repository
    • Credits
    • Benchmark
Powered by GitBook
On this page
  • Find
  • Example
  • First / Last
  • Offset and Limit
  • Offset
  1. Querying
  2. The collection object
  3. Filter the query

Find, First, Last, Offset, Limit

You may want to fetch one model instead of a collection.

Find

Collection#find allows to fetch a model based on an expression.

There's two flavors for find method: find and find! . The first one return a nilable type, which will be nil if not found, while the second return a model or throw an exception if not found.

Example

p = Product.query.find({id: 1234}) # Return Product?

p = Product.query.find!{ id == 1234 } # Return Product or throw an exception if not found.

First / Last

First and last return the first and last row of a SELECT query.

In the case of first, it will order by [primary key column] ASC if no order_by directive is found. In the case of last, it will invert the direction of the order directive, turning each ASC to DESC and vice-versa before performing the call.

Both return a model instead of an enumeration of models.

# SELECT * FROM products ORDER BY created_at ASC LIMIT 1
p = Product.query.order_by("created_at", "DESC").last!

# SELECT * FROM products ORDER BY created_at DESC LIMIT 1
p = Product.query.order_by("created_at", "DESC").first!

Like with find, first!/first and last/last! are existing variant of the method

Offset and Limit

Offset and limit provide a way to scope a request or do some pagination.

Offset

Product.query.order_by("id").limit(5).offset(5)

The code above will fetch the model from position 5 .. 10 of the query.

It is possible to write the same behavior as above by using [] operator:

products = Product.query.order_by("id")[5..10]

Nothing to be aware: [] operator will resolve the query, calling it and return an Array of model, not a Collection object anymore.

You may use the [] operator with a number as parameter instead of range. In this case, it's equivalent to offset(number).first!. The []? operator is equivalent to offset(number).first and will return nilable reference.

PreviousFilter the query – The Expression EngineNextAggregation

Last updated 5 years ago