module AcceptsNestedIds

Allows a model to accept nested association IDs as an attribute. Normally these associations would be immediately saved, even if the parent model transaction failed. This class defers saving to after_save, and also provides dirty attribute tracking on the parent model.

Constants

VERSION

Public Instance Methods

save_nested_id_associations() click to toggle source

Defered association setter

@example Method definition

if @document_ids
  self.documents = Document.where(id: document_ids)
end
# File lib/accepts_nested_ids.rb, line 26
def save_nested_id_associations
  aggregated_id_associations = self.class.nested_id_associations | self.class.base_class.nested_id_associations
  aggregated_id_associations.each do |nested_id_association|
    if instance_variable_get("@#{nested_id_association.ids_attr}")
      association_class = nested_id_association.class_name.constantize
      ids               = send(nested_id_association.ids_attr)
      send("#{nested_id_association.attr}=", association_class.where(id: ids))
    end
  end
end