Class: Simple::SQL::Result::Records
- Inherits:
-
Simple::SQL::Result
- Object
- Array
- Simple::SQL::Result
- Simple::SQL::Result::Records
- Defined in:
- lib/simple/sql/result/records.rb
Constant Summary collapse
- Reflection =
::Simple::SQL::Reflection
- AssociationLoader =
– preload associations ————————————————-
::Simple::SQL::Result::AssociationLoader
Instance Attribute Summary
Attributes inherited from Simple::SQL::Result
#current_page, #total_count, #total_pages
Instance Method Summary collapse
-
#initialize(records, target_type:, pg_source_oid:) ⇒ Records
constructor
:nodoc:.
-
#preload(association, as: nil, order_by: nil, limit: nil) ⇒ Object
Preloads an association.
Methods inherited from Simple::SQL::Result
Constructor Details
#initialize(records, target_type:, pg_source_oid:) ⇒ Records
:nodoc:
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/simple/sql/result/records.rb', line 9 def initialize(records, target_type:, pg_source_oid:) # :nodoc: # expect! records.first => Hash unless records.empty? super(records) @hash_records = records @target_type = target_type @pg_source_oid = pg_source_oid @associations = [] materialize end |
Instance Method Details
#preload(association, as: nil, order_by: nil, limit: nil) ⇒ Object
Preloads an association.
This can now be used as follows:
scope = SQL::Scope.new("SELECT * FROM users")
results = SQL.all scope, into: :struct
results.preload(:organization)
The preload method uses foreign key definitions in the database to figure out which table to load from.
This method is only available if into:
was set in the call to SQL.all
. It raises an error otherwise.
Parameters:
-
association: the name of the association.
-
as: the target name of the association.
-
order_by: if set describes ordering; see Scope#order_by.
-
limit: if set describes limits; see Scope#order_by.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/simple/sql/result/records.rb', line 46 def preload(association, as: nil, order_by: nil, limit: nil) expect! association => Symbol expect! as => [nil, Symbol] # resolve oid into table and schema name. # # [TODO] is this still correct? schema, host_table = Reflection.lookup_pg_class @pg_source_oid AssociationLoader.preload @hash_records, association, host_table: host_table, schema: schema, as: as, order_by: order_by, limit: limit @associations << association materialize end |