Class: ActiveRecord::Associations::HasAndBelongsToManyAssociation
- Inherits:
-
Object
- Object
- ActiveRecord::Associations::HasAndBelongsToManyAssociation
- Defined in:
- lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb
Instance Method Summary collapse
- #delete_records(records, method) ⇒ Object
- #insert_record(record, validate = true, raise = false) ⇒ Object
Instance Method Details
#delete_records(records, method) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb', line 40 def delete_records(records, method) if sql = [:delete_sql] records.each { |record| owner.class.connection.delete(interpolate(sql, record)) } else relation = join_table # CPK # stmt = relation.where(relation[reflection.foreign_key].eq(owner.id). # and(relation[reflection.association_foreign_key].in(records.map { |x| x.id }.compact)) #).compile_delete predicate1 = cpk_id_predicate(relation, Array(reflection.foreign_key), Array(owner.id)) predicate2 = cpk_in_predicate(relation, Array(reflection.association_foreign_key), records.map { |x| x.id }) unless records == :all stmt = relation.where(predicate1.and(predicate2)).compile_delete owner.class.connection.delete stmt.to_sql end end |
#insert_record(record, validate = true, raise = false) ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/composite_primary_keys/associations/has_and_belongs_to_many_association.rb', line 4 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 if [:insert_sql] owner.class.connection.insert(interpolate([:insert_sql], record)) else # CPK #stmt = join_table.compile_insert( # join_table[reflection.foreign_key] => owner.id, # join_table[reflection.association_foreign_key] => record.id #) join_values = Hash.new Array(reflection.foreign_key).zip(Array(owner.id)) do |name, value| attribute = join_table[name] join_values[attribute] = value end Array(reflection.association_foreign_key).zip(Array(record.id)) do |name, value| attribute = join_table[name] join_values[attribute] = value end stmt = join_table.compile_insert(join_values) owner.class.connection.insert stmt.to_sql end record end |