Flowdock
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 36
        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_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 = columns.inject({}) do |attrs, column|
              name = column.name
              case name.to_s
                when @reflection.primary_key_name.to_s
                  attrs[relation[name]] = @owner.id
                when @reflection.association_foreign_key.to_s
                  attrs[relation[name]] = record.id
                when *timestamps
                  attrs[relation[name]] = timezone
                else
                  if record.has_attribute?(name)
                    value = @owner.send(:quote_value, record[name], column)
                    attrs[relation[name]] = value unless value.nil?
                  end
              end
              attrs
            end

            relation.insert(attributes)
          end

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