Flowdock
method

insert_record

Importance_0
v3.0.9 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::HasAndBelongsToManyAssociation
insert_record(record, force = true, validate = true) protected

No documentation

This method has no description. You can help the Ruby on Rails community by adding new notes.

Hide source
# File activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb, line 45
        def insert_record(record, force = true, validate = true)
          if record.new_record?
            if force
              record.save!
            else
              return false unless record.save(:validate => validate)
            end
          end

          if @reflection.options[:insert_sql]
            @owner.connection.insert(interpolate_and_sanitize_sql(@reflection.options[:insert_sql], record))
          else
            relation   = Arel::Table.new(@reflection.options[:join_table])
            timestamps = record_timestamp_columns(record)
            timezone   = record.send(:current_time_from_proper_timezone) if timestamps.any?

            attributes = Hash[columns.map do |column|
              name = column.name
              value = case name.to_s
                when @reflection.primary_key_name.to_s
                  @owner.id
                when @reflection.association_foreign_key.to_s
                  record.id
                when *timestamps
                  timezone
                else
                  @owner.send(:quote_value, record[name], column) if record.has_attribute?(name)
              end
              [relation[name], value] unless value.nil?
            end]

            relation.insert(attributes)
          end

          return true
        end
Register or log in to add new notes.