class Zn::Associators::HasMany

HasMany associator helps define has_many relations between objects

Attributes

dataset[R]
dataset_name[R]
datasets[R]
foreign_key[R]
primary_key[R]
repo[R]

Public Class Methods

new(datasets, params) click to toggle source
# File lib/zn/associators/has_many.rb, line 9
def initialize(datasets, params)
  raise ArgumentError, 'Datasets is nil' if datasets.nil?

  @datasets = datasets
  @dataset_name = params.fetch('dataset')
  @foreign_key = params.fetch('foreign_key')
  @primary_key = params.fetch('primary_key')
end

Public Instance Methods

associate(object) click to toggle source
# File lib/zn/associators/has_many.rb, line 18
def associate(object)
  @dataset ||= datasets.fetch(dataset_name)

  dataset.search(key: foreign_key, value: object.fetch(primary_key), exact: true).to_a
end