Module: PDC::Resource::Associations::ClassMethods

Defined in:
lib/pdc/resource/associations.rb

Instance Method Summary collapse

Instance Method Details

#has_many(name, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pdc/resource/associations.rb', line 15

def has_many(name, options = {})
  create_association(name, HasMany, options)

  define_method "#{name.to_s.singularize}_ids=" do |ids|
    attributes[name] = []
    ids.reject(&:blank?).each { |id| association(name).build(id: id) }
  end

  define_method "#{name.to_s.singularize}_ids" do
    association(name).map(&:id)
  end
end

#reflect_on_association(name) ⇒ Object



28
29
30
# File 'lib/pdc/resource/associations.rb', line 28

def reflect_on_association(name)
  associations[name] || associations[name.to_s.pluralize.to_sym]
end