Sha256: 6dff40579bbd2dce305823d136604237922146fe2c4e8333ad3da8f983b9867b
Contents?: true
Size: 1.26 KB
Versions: 6
Compression:
Stored size: 1.26 KB
Contents
# encoding: utf-8 module Dynamoid #:nodoc: # The has and belongs to many association. module Associations class HasAndBelongsToMany include ManyAssociation private # Find the target association, always another :has_and_belongs_to_many association. Uses either options[:inverse_of] or the source class name # and default parsing to return the most likely name for the target association. # # @since 0.2.0 def target_association key_name = options[:inverse_of] || source.class.to_s.pluralize.underscore.to_sym guess = target_class.associations[key_name] return nil if guess.nil? || guess[:type] != :has_and_belongs_to_many key_name end # Associate a source object to this association. # # @since 0.2.0 def associate_target(object) ids = object.send(target_attribute) || Set.new object.update_attribute(target_attribute, ids.merge(Array(source.hash_key))) end # Disassociate a source object from this association. # # @since 0.2.0 def disassociate_target(object) ids = object.send(target_attribute) || Set.new object.update_attribute(target_attribute, ids - Array(source.hash_key)) end end end end
Version data entries
6 entries across 6 versions & 2 rubygems