class Filemaker::Model::Relations::HasMany

Public Class Methods

new(owner, name, options) click to toggle source
Calls superclass method
# File lib/filemaker/model/relations/has_many.rb, line 7
def initialize(owner, name, options)
  super(owner, name, options)
  build_target
end

Public Instance Methods

<<(*args) click to toggle source

Append a model or array of models to the relation. Will set the owner ID to the children.

@example Append a model

job.applicants << applicant

@example Array of models

job.applicants << [applicant_a, applicant_b, applicant_c]

@param [Filemaker::Model, Array<Filemaker::Model>] *args

# File lib/filemaker/model/relations/has_many.rb, line 42
def <<(*args)
  docs = args.flatten
  return concat(docs) if docs.size > 1

  if (doc = docs.first)
    create(doc)
  end
  self
end
Also aliased as: push
build(attrs = {}) click to toggle source

Build a single model. The owner will be linked, but the record will not be saved.

@example Append a model

job.applicants.build(name: 'Bob')
job.save

@param [Hash] attrs The attributes for the fields

@return [Filemaker::Model] the actual model

# File lib/filemaker/model/relations/has_many.rb, line 67
def build(attrs = {})
  # attrs.merge!(owner.identity.name => owner.identity_id) if \
  #   owner.identity_id
  #
  attrs[owner.identity.name] = owner.identity_id if owner.identity_id
  target_class.new(attrs)
end
create(attrs = {}) click to toggle source

Same as `build`, except that it will be saved automatically.

@return [Filemaker::Model] the actual saved model

# File lib/filemaker/model/relations/has_many.rb, line 78
def create(attrs = {})
  build(attrs).save
end
final_reference_key() click to toggle source
# File lib/filemaker/model/relations/has_many.rb, line 26
def final_reference_key
  target_class.find_field_by_name(source_key).try(:name) ||
    target_class.find_field_by_name(reference_key).try(:name) ||
    target_class.identity.try(:name)
end
push(*args)
Alias for: <<
reference_key() click to toggle source

If no reference_key, we will use owner's identity field. If there is no identity, we will…??

# File lib/filemaker/model/relations/has_many.rb, line 14
def reference_key
  options.fetch(:reference_key) { owner.identity.name }
end
reference_value() click to toggle source
# File lib/filemaker/model/relations/has_many.rb, line 18
def reference_value
  owner.public_send(reference_key.to_sym)
end
source_key() click to toggle source
# File lib/filemaker/model/relations/has_many.rb, line 22
def source_key
  options.fetch(:source_key) { nil }
end

Protected Instance Methods

build_target() click to toggle source
# File lib/filemaker/model/relations/has_many.rb, line 84
def build_target
  @target = if reference_value.blank? || final_reference_key.blank?
              []
            else
              target_class.where(
                final_reference_key => "==#{reference_value}"
              )
            end
end