Class: DatastaxRails::Associations::HasAndBelongsToManyAssociation

Inherits:
CollectionAssociation show all
Defined in:
lib/datastax_rails/associations/has_and_belongs_to_many_association.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from CollectionAssociation

#proxy

Attributes inherited from Association

#loaded, #owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from CollectionAssociation

#add_to_target, #any?, #build, #concat, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #empty?, #ids_reader, #ids_writer, #load_target, #many?, #reader, #replace, #reset, #size, #uniq, #writer

Methods inherited from Association

#aliased_column_family, #association_scope, #klass, #load_target, #loaded!, #reload, #reset, #reset_scope, #scoped, #set_inverse_instance, #stale_target?, #target_scope

Constructor Details

#initialize(owner, reflection) ⇒ HasAndBelongsToManyAssociation

Returns a new instance of HasAndBelongsToManyAssociation.



7
8
9
10
# File 'lib/datastax_rails/associations/has_and_belongs_to_many_association.rb', line 7

def initialize(owner, reflection)
  self.join_name = [owner.class.name.underscore, reflection.class_name.underscore].sort.join('_')
  super
end

Instance Attribute Details

#join_nameObject (readonly)

Returns the value of attribute join_name.



5
6
7
# File 'lib/datastax_rails/associations/has_and_belongs_to_many_association.rb', line 5

def join_name
  @join_name
end

Instance Method Details

#insert_record(record, validate = true, raise = false) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/datastax_rails/associations/has_and_belongs_to_many_association.rb', line 12

def insert_record(record, validate = true, raise = false)
  if record.new_record?
    if raise
      record.save!(validate: validate)
    else
      return unless record.save(validate: validate)
    end
  end

  left, right = [[record.class.name, record.id], [owner.class.name, owner.id]].sort { |a, b| b.first <=> a.first }

  DatastaxRails::Base.connection.insert(join_column_family,
                                        SimpleUUID::UUID.new.to_guid,
                                        left:  "#{join_name}:#{left.last}",
                                        right: "#{join_name}:#{right.last}")
end