Class: Filemaker::Model::Relations::HasMany

Inherits:
Proxy
  • Object
show all
Defined in:
lib/filemaker/model/relations/has_many.rb

Instance Attribute Summary

Attributes inherited from Proxy

#options, #owner, #target

Instance Method Summary collapse

Methods inherited from Proxy

#method_missing, #target_class

Constructor Details

#initialize(owner, name, options) ⇒ HasMany

Returns a new instance of HasMany.



7
8
9
10
# File 'lib/filemaker/model/relations/has_many.rb', line 7

def initialize(owner, name, options)
  super(owner, name, options)
  build_target
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Filemaker::Model::Relations::Proxy

Instance Method Details

#<<(*args) ⇒ Object Also known as: push

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

Examples:

Append a model

job.applicants << applicant

Array of models

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

Parameters:



28
29
30
31
32
33
34
35
# File 'lib/filemaker/model/relations/has_many.rb', line 28

def <<(*args)
  docs = args.flatten
  return concat(docs) if docs.size > 1
  if (doc = docs.first)
    create(doc)
  end
  self
end

#build(attrs = {}) ⇒ Filemaker::Model

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

Examples:

Append a model

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

Parameters:

  • attrs (Hash) (defaults to: {})

    The attributes for the fields

Returns:



52
53
54
55
56
57
58
# File 'lib/filemaker/model/relations/has_many.rb', line 52

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 = {}) ⇒ Filemaker::Model

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

Returns:



63
64
65
# File 'lib/filemaker/model/relations/has_many.rb', line 63

def create(attrs = {})
  build(attrs).save
end

#reference_keyObject

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



14
15
16
# File 'lib/filemaker/model/relations/has_many.rb', line 14

def reference_key
  options.fetch(:reference_key) { owner.identity.name }
end