class ActiveRecord::Associations::CollectionAssociation

Public Instance Methods

build(attributes = {}, options = {}) { |record| ... } click to toggle source
# File lib/active_record/mass_assignment_security/associations.rb, line 15
def build(attributes = {}, options = {}, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| build(attr, options, &block) }
  else
    add_to_target(build_record(attributes, options)) do |record|
      yield(record) if block_given?
    end
  end
end
create(attributes = {}, options = {}, &block) click to toggle source
# File lib/active_record/mass_assignment_security/associations.rb, line 25
def create(attributes = {}, options = {}, &block)
  create_record(attributes, options, &block)
end
create!(attributes = {}, options = {}, &block) click to toggle source
# File lib/active_record/mass_assignment_security/associations.rb, line 29
def create!(attributes = {}, options = {}, &block)
  create_record(attributes, options, true, &block)
end

Private Instance Methods

create_record(attributes, options, raise = false) { |record| ... } click to toggle source
# File lib/active_record/mass_assignment_security/associations.rb, line 33
def create_record(attributes, options, raise = false, &block)
  unless owner.persisted?
    raise ActiveRecord::RecordNotSaved, "You cannot call create unless the parent is saved"
  end

  if attributes.is_a?(Array)
    attributes.collect { |attr| create_record(attr, options, raise, &block) }
  else
    transaction do
      add_to_target(build_record(attributes, options)) do |record|
        yield(record) if block_given?
        insert_record(record, true, raise)
      end
    end
  end
end