Module: OccamsRecord
- Defined in:
- lib/occams-record/version.rb,
lib/occams-record/merge.rb,
lib/occams-record/query.rb,
lib/occams-record/batches.rb,
lib/occams-record/results.rb,
lib/occams-record/raw_query.rb,
lib/occams-record/eager_loaders.rb,
lib/occams-record/eager_loaders/base.rb,
lib/occams-record/eager_loaders/habtm.rb,
lib/occams-record/eager_loaders/has_one.rb,
lib/occams-record/eager_loaders/has_many.rb,
lib/occams-record/eager_loaders/belongs_to.rb,
lib/occams-record/eager_loaders/polymorphic_belongs_to.rb
Overview
Main entry point for using OccamsRecord.
Defined Under Namespace
Modules: Batches, EagerLoaders, Results Classes: Merge, Query, RawQuery
Constant Summary collapse
- VERSION =
Library version
'0.13.1'.freeze
Class Method Summary collapse
-
.query(scope, use: nil, query_logger: nil) ⇒ OccamsRecord::Query
Starts building a OccamsRecord::Query.
-
.sql(sql, binds, use: nil, query_logger: nil) ⇒ OccamsRecord::RawQuery
Starts building a OccamsRecord::RawQuery.
Class Method Details
.query(scope, use: 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 us ActiveRecord for it. Instead, use OccamsRecord::Query#eager_load. Finally, call ‘run` 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
25 26 27 |
# File 'lib/occams-record/query.rb', line 25 def self.query(scope, use: nil, query_logger: nil) Query.new(scope, use: use, query_logger: query_logger) end |
.sql(sql, binds, use: nil, query_logger: nil) ⇒ OccamsRecord::RawQuery
Starts building a OccamsRecord::RawQuery. Pass it a raw SQL statement, optionally followed by a Hash of binds. While this doesn’t offer an additional performance boost, it’s a nice way to write safe, complicated SQL by hand while also supporting eager loading.
results = OccamsRecord.sql(%(
SELECT * FROM widgets
WHERE category_id = %{cat_id}
), {
cat_id: 5
}).run
If you want to do eager loading, you must first the define a model to pull the associations from. NOTE If you’re using SQLite, you must always specify the model.
results = OccamsRecord.
sql(%(
SELECT * FROM widgets
WHERE category_id IN (%{cat_ids})
), {
cat_ids: [5, 10]
}).
model(Widget).
eager_load(:category).
run
NOTE To use find_each/find_in_batches, your SQL string must include ‘LIMIT %batch_limit OFFSET %batch_offset’, and an ORDER BY is strongly recomended. OccamsRecord will provide the bind values for you.
38 39 40 |
# File 'lib/occams-record/raw_query.rb', line 38 def self.sql(sql, binds, use: nil, query_logger: nil) RawQuery.new(sql, binds, use: use, query_logger: nil) end |