Class: Restforce::DB::Associations::HasMany

Inherits:
ForeignKey show all
Defined in:
lib/restforce/db/associations/has_many.rb

Overview

Restforce::DB::Associations::HasMany defines a relationship in which potentially several Salesforce records maintain a reference to the Salesforce record on the current Mapping.

Instance Attribute Summary

Attributes inherited from Base

#lookup, #name

Instance Method Summary collapse

Methods inherited from ForeignKey

#fields

Methods inherited from Base

#fields, #initialize, #synced_for?

Constructor Details

This class inherits a constructor from Restforce::DB::Associations::Base

Instance Method Details

#build(database_record, salesforce_record) ⇒ Object

Public: Construct a database record for each Salesforce record associated with the supplied parent Salesforce record.

database_record - An instance of an ActiveRecord::Base subclass. salesforce_record - A Hashie::Mash representing a Salesforce object.

Returns an Array of constructed association records.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/restforce/db/associations/has_many.rb', line 19

def build(database_record, salesforce_record)
  target = target_mapping(database_record)
  lookup_id = "#{lookup_field(target, database_record)} = '#{salesforce_record.Id}'"

  records = []
  target.salesforce_record_type.each(conditions: lookup_id) do |instance|
    records << construct_for(database_record, instance)
  end

  records.flatten
end