Method: OccamsRecord.query

Defined in:
lib/occams-record/query.rb

.query(scope, use: nil, active_record_fallback: nil, query_logger: nil) ⇒ OccamsRecord::Query

Starts building a OccamsRecord::Query. Pass it a scope from any of ActiveRecord’s query builder methods or associations. If you want to eager loaded associations, do NOT use ActiveRecord for it. Instead use OccamsRecord::Query#eager_load. Finally, call ‘run` (or any Enumerable method) to run the query and get back an array of objects.

results = OccamsRecord
  .query(Widget.order("name"))
  .eager_load(:category)
  .eager_load(:order_items, ->(q) { q.select("widget_id, order_id") }) {
    eager_load(:orders) {
      eager_load(:customer, ->(q) { q.select("name") })
    }
  }
  .run

Parameters:

  • scope (ActiveRecord::Relation)
  • use (Module) (defaults to: nil)

    optional Module to include in the result class

  • query_logger (Array) (defaults to: nil)

    (optional) an array into which all queries will be inserted for logging/debug purposes

  • active_record_fallback (Symbol) (defaults to: nil)

    If passed, missing methods will be forwarded to an ActiveRecord instance. Options are :lazy (allow lazy loading in the AR record) or :strict (require strict loading)

Returns:



24
25
26
# File 'lib/occams-record/query.rb', line 24

def self.query(scope, use: nil, active_record_fallback: nil, query_logger: nil)
  Query.new(scope, use: use, query_logger: query_logger, active_record_fallback: active_record_fallback)
end