class Zn::Associators::BelongsTo
BelongsTo
associator helps define belongs_to 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/belongs_to.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/belongs_to.rb, line 18 def associate(object) @dataset ||= datasets.fetch(dataset_name) dataset.search(key: primary_key, value: object.fetch(foreign_key), exact: true).to_a.first end