Sha256: 707a47f69d0b6e31a358fff28faa19b0209b65b4b8239981f722b1018c5a6086
Contents?: true
Size: 1.41 KB
Versions: 2
Compression:
Stored size: 1.41 KB
Contents
module CompositePrimaryKeys module CollectionAssociation def ids_writer(ids) primary_key = reflection.association_primary_key ids = Array(ids).reject(&:blank?) # CPK- if primary_key.is_a?(Array) ids = ids.map { |id| CompositePrimaryKeys::CompositeKeys.parse(id) } primary_key.each_with_index do |key, i| pk_type = klass.type_for_attribute(key) ids.each { |id| id[i] = pk_type.cast(id[i]) } end predicate = CompositePrimaryKeys::Predicates.cpk_in_predicate(klass.arel_table, reflection.association_primary_key, ids) records = klass.where(predicate).index_by do |r| reflection.association_primary_key.map{ |k| r.send(k) } end.values_at(*ids).compact else pk_type = klass.type_for_attribute(primary_key) ids.map! { |i| pk_type.cast(i) } records = klass.where(primary_key => ids).index_by do |r| r.public_send(primary_key) end.values_at(*ids).compact end if records.size != ids.size found_ids = records.map { |record| record.public_send(primary_key) } not_found_ids = ids - found_ids klass.all.raise_record_not_found_exception!(ids, records.size, ids.size, primary_key, not_found_ids) else replace(records) end end end end ActiveRecord::Associations::CollectionAssociation.prepend CompositePrimaryKeys::CollectionAssociation
Version data entries
2 entries across 2 versions & 1 rubygems