class Restforce::DB::Associations::HasMany

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

Public Instance Methods

build(database_record, salesforce_record, cache = AssociationCache.new(database_record)) click to toggle source

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. cache - A Restforce::DB::AssociationCache (optional).

Returns an Array of constructed association records.

# File lib/restforce/db/associations/has_many.rb, line 20
def build(database_record, salesforce_record, cache = AssociationCache.new(database_record))
  return [] unless build?

  @cache = cache

  reflection = target_reflection(database_record)
  records = []

  target_mappings(database_record).each do |target|
    lookup_id = "#{lookup_field(target, reflection)} = '#{salesforce_record.Id}'"

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

  records.flatten
ensure
  @cache = nil
end

Private Instance Methods

construction_method() click to toggle source

Internal: Get the method by which an associated record should be assigned to this record. Replaces :writer with :concat, which appends records to an existing association, rather than replacing it.

Returns a Symbol.

# File lib/restforce/db/associations/has_many.rb, line 48
def construction_method
  :concat
end