class ActiveRecord::Associations::HasManyThroughAssociation

Public Instance Methods

build_through_record(record) click to toggle source
# File lib/active_record/mass_assignment_security/associations.rb, line 109
def build_through_record(record)
  @through_records[record] ||= begin
    ensure_mutable

    attributes = through_scope_attributes
    attributes[source_reflection.name] = record
    attributes[source_reflection.foreign_type] = options[:source_type] if options[:source_type]

    # Pass in `without_protection: true` here because `options_for_through_record`
    # was removed in https://github.com/rails/rails/pull/35799
    through_association.build(attributes, without_protection: true)
  end
end

Private Instance Methods

build_record(attributes, options = {}) click to toggle source
Calls superclass method
# File lib/active_record/mass_assignment_security/associations.rb, line 142
def build_record(attributes, options = {})
  ensure_not_nested

  record = super(attributes, options)

  inverse = source_reflection.inverse_of
  if inverse
    if inverse.macro == :has_many
      record.send(inverse.name) << build_through_record(record)
    elsif inverse.macro == :has_one
      record.send("#{inverse.name}=", build_through_record(record))
    end
  end

  record
end
options_for_through_record() click to toggle source
# File lib/active_record/mass_assignment_security/associations.rb, line 161
def options_for_through_record
  [through_scope_attributes, without_protection: true]
end