method

insert_record

Importance_0
v2.1.0 - Show latest stable - 0 notes - Class: ActiveRecord::Associations::HasAndBelongsToManyAssociation
insert_record(record, force=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 23
        def insert_record(record, force=true)
          if record.new_record?
            if force
              record.save!
            else
              return false unless record.save
            end
          end

          if @reflection.options[:insert_sql]
            @owner.connection.insert(interpolate_sql(@reflection.options[:insert_sql], record))
          else
            columns = @owner.connection.columns(@reflection.options[:join_table], "#{@reflection.options[:join_table]} Columns")

            attributes = columns.inject({}) do |attrs, column|
              case column.name.to_s
                when @reflection.primary_key_name.to_s
                  attrs[column.name] = @owner.quoted_id
                when @reflection.association_foreign_key.to_s
                  attrs[column.name] = record.quoted_id
                else
                  if record.has_attribute?(column.name)
                    value = @owner.send(:quote_value, record[column.name], column)
                    attrs[column.name] = value unless value.nil?
                  end
              end
              attrs
            end

            sql =
              "INSERT INTO #{@owner.connection.quote_table_name @reflection.options[:join_table]} (#{@owner.send(:quoted_column_names, attributes).join(', ')}) " +
              "VALUES (#{attributes.values.join(', ')})"

            @owner.connection.insert(sql)
          end

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