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
  • Collection(T)#each
  • Collection(T)#fetch
  1. Querying
  2. The collection object
  3. Fetching the query

Each and Fetch

PreviousFetching the queryNextCursored fetching

Last updated 6 years ago

Collection inherits from therefore it allows all the methods defined by the module. When calling enumeration via each or mapor any other methods defined in Enumerable(T), the collection is resolved and SQL request is triggered.

Collection(T)#each

Return the list of models returned by the request:

Post.query.where(user_id: 1).each do |posts|
    # Do something with the posts
end

Collection(T)#fetch

Fetch stands for iterating through hash instead of model. While offering less features (as we do not connect a model to the data), it offers best performances, as no extra-allocations are made:

Post.query.where(user_id: 1).fetch do |posts|
    puts "#{post["id"]} - #{post["name"]}"
end
Enumerable(T)