Class: Rasti::DB::Relations::ManyToOne
- 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_key ⇒ Object
90 91 92 |
# File 'lib/rasti/db/relations.rb', line 90 def foreign_key @foreign_key ||= @options[:foreign_key] || target_collection_class.implicit_foreign_key_name end |
#graph_to(rows, db, schema = nil, relations = []) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rasti/db/relations.rb', line 94 def graph_to(rows, db, schema=nil, relations=[]) fks = rows.map { |row| row[foreign_key] }.uniq target_collection = target_collection_class.new db, schema relation_rows = target_collection.where(source_collection_class.primary_key => fks) .graph(*relations) .each_with_object({}) do |row, hash| hash[row.public_send(source_collection_class.primary_key)] = row end rows.each do |row| row[name] = relation_rows[row[foreign_key]] end end |