Method: ActiveRecord::Base.create_temporary_table

Defined in:
lib/ar-extensions/temporary_table.rb

.create_temporary_table(options = {}) ⇒ Object

Creates a temporary table given the passed in options hash. The temporary table is created based off from another table the current model class. This method returns the constant for the new new model. This can also be used with block form (see below).

Parameters

  • options - the options hash used to define the temporary table.

Options

  • :table_name - the desired name of the temporary table. If not supplied \ then a name of “temp_” + the current table_name of the current model \ will be used.

  • :like - the table model you want to base the temporary tables \ structure off from. If this is not supplied then the table_name of the \ current model will be used.

  • :model_name - the name of the model you want to use for the temporary \ table. This must be compliant with Ruby’s naming conventions for \ constants. If this is not supplied a rails-generated table name will \ be created which is based off from the table_name of the temporary table. \ IE: Account.create_temporary_table creates the TempAccount model class

Example 1, using defaults

class Project < ActiveRecord::Base ; end

Project.create_temporary_table

This creates a temporary table named ‘temp_projects’ and creates a constant name TempProject. The table structure is copied from the projects table.

Example 2, using :table_name and :model options

Project.create_temporary_table :table_name=>'my_projects', :model=>'MyProject'

This creates a temporary table named ‘my_projects’ and creates a constant named MyProject. The table structure is copied from the projects table.

Example 3, using :like

ActiveRecord::Base.create_temporary_table :like=>Project

This is the same as calling Project.create_temporary_table.

Example 4, using block form

Project.create_temporary_table do |t|
  # ...
end

Using the block form will automatically drop the temporary table when the block exits. t which is passed into the block is the temporary table class. In the above example t equals TempProject. The block form can be used with all of the available options.

See

  • drop

Raises:

  • (Exception)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ar-extensions/temporary_table.rb', line 73

def self.create_temporary_table( options={} )
  options[:table_name] = "temp_#{self.table_name}" unless options[:table_name]
  options[:like] = self unless options[:like]	
  options[:temporary] = true if not options[:permanent] and not options.has_key?( :temporary )
  table_name = options[:table_name]
  model_name = options[:model_name] || ActiveSupport::Inflector.classify( table_name )	
  raise Exception.new( "Model #{model_name} already exists! \n" ) if Object.const_defined? model_name 
  
  like_table_name = options[:like].table_name || self.table_name
  sql = "CREATE #{options[:temporary] ? 'TEMPORARY' : ''} TABLE #{table_name} LIKE #{like_table_name}"
  connection.execute( sql )		
  
  eval "class ::#{model_name} < #{ActiveRecord::TemporaryTable.name}
			set_table_name :#{table_name}
		end"

  @@temporary_table_hsh[ model = Object.const_get( model_name ) ] = true
  if block_given?
    yield model
    model.drop
    nil
  else
    model
  end
end