class ActiveRecord::Associations::HasManyForActiveModelAssociation

Public Instance Methods

concat(*records) click to toggle source

no need transaction

# File lib/active_record/associations/has_many_for_active_model_association.rb, line 44
def concat(*records)
  load_target
  flatten_records = records.flatten
  flatten_records.each { |val| raise_on_type_mismatch!(val) }
  target_ids = reflection.options[:target_ids]
  owner[target_ids] ||= []
  owner[target_ids].concat(flatten_records.map(&:id))

  flatten_records.each do |record|
    if index = @target.index(record)
      @target[index] = record
    else
      @target << record
    end
  end

  target
end
empty?() click to toggle source

not support counter_cache

# File lib/active_record/associations/has_many_for_active_model_association.rb, line 14
def empty?
  if loaded?
    size.zero?
  else
    @target.blank? && !scope.exists?
  end
end
find_target?() click to toggle source

remove conditions: owner.new_record?, foreign_key_present?

# File lib/active_record/associations/has_many_for_active_model_association.rb, line 4
def find_target?
  !loaded? && klass
end
null_scope?() click to toggle source

no dependent action

# File lib/active_record/associations/has_many_for_active_model_association.rb, line 9
def null_scope?
  false
end
replace(other_array) click to toggle source

full replace simplely

# File lib/active_record/associations/has_many_for_active_model_association.rb, line 23
def replace(other_array)
  original_target = load_target.dup
  other_array.each { |val| raise_on_type_mismatch!(val) }
  target_ids = reflection.options[:target_ids]
  owner[target_ids] = other_array.map(&:id)

  old_records = original_target - other_array
  old_records.each do |record|
    @target.delete(record)
  end

  other_array.each do |record|
    if index = @target.index(record)
      @target[index] = record
    else
      @target << record
    end
  end
end

Private Instance Methods

get_records() click to toggle source
# File lib/active_record/associations/has_many_for_active_model_association.rb, line 65
def get_records
  return scope.to_a if reflection.scope_chain.any?(&:any?)

  target_ids = reflection.options[:target_ids]
  klass.where(id: owner[target_ids]).to_a
end