Class: Rasti::DB::Relations::OneToMany

Inherits:
Base
  • Object
show all
Defined in:
lib/rasti/db/relations.rb

Instance Attribute Summary

Attributes inherited from Base

#name, #source_collection_class

Instance Method Summary collapse

Methods inherited from Base

#initialize, #many_to_many?, #many_to_one?, #one_to_many?, #target_collection_class

Constructor Details

This class inherits a constructor from Rasti::DB::Relations::Base

Instance Method Details

#foreign_keyObject



67
68
69
# File 'lib/rasti/db/relations.rb', line 67

def foreign_key
  @foreign_key ||= @options[:foreign_key] || source_collection_class.implicit_foreign_key_name
end

#graph_to(rows, db, schema = nil, relations = []) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rasti/db/relations.rb', line 71

def graph_to(rows, db, schema=nil, relations=[])
  pks = rows.map { |row| row[source_collection_class.primary_key] }.uniq

  target_collection = target_collection_class.new db, schema

  relation_rows = target_collection.where(foreign_key => pks)
                                   .graph(*relations)
                                   .group_by { |r| r.public_send(foreign_key) }

  rows.each do |row| 
    row[name] = relation_rows.fetch row[source_collection_class.primary_key], []
  end
end