instance method
change_table
Ruby on Rails 2.3.18
Since v2.2.3Signature
change_table(table_name)
A block for changing columns in table.
Example
# change_table() yields a Table instance change_table(:suppliers) do |t| t.column :name, :string, :limit => 60 # Other column alterations here end
Examples
Add a column
change_table(:suppliers) do |t| t.column :name, :string, :limit => 60 end
Add 2 integer columns
change_table(:suppliers) do |t| t.integer :width, :height, :null => false, :default => 0 end
Add created_at/updated_at columns
change_table(:suppliers) do |t| t.timestamps end
Add a foreign key column
change_table(:suppliers) do |t| t.references :company end
Creates a company_id(integer) column
Add a polymorphic foreign key column
change_table(:suppliers) do |t| t.belongs_to :company, :polymorphic => true end
Creates company_type(varchar) and company_id(integer) columns
Remove a column
change_table(:suppliers) do |t| t.remove :company end
Remove several columns
change_table(:suppliers) do |t| t.remove :company_id t.remove :width, :height end
Remove an index
change_table(:suppliers) do |t| t.remove_index :company_id end
See also Table for details on all of the various column transformation
Parameters
-
table_namereq
Source
# File activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb, line 169
def change_table(table_name)
yield Table.new(table_name, self)
end
Defined in activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb line 169
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::ConnectionAdapters::SchemaStatements