add_column
![Very extensive documentation Importance_5](https://d2vfyqvduarcvs.cloudfront.net/images/importance_5.png?1349367920)
add_column(table_name, column_name, type, options = {})
public
Adds a new column to the named table. See TableDefinition#column for details of the options you can use.
![Default_avatar_30](https://www.gravatar.com/avatar/f4f96a08152550e603ee595403f3ba57?default=http://apidock.com/images/default_avatar_30.png&size=30)
![Default_avatar_30](https://www.gravatar.com/avatar/3eca00a0d3f5725d81723b69621287bd?default=http://apidock.com/images/default_avatar_30.png&size=30)
script/generate syntax
To add a post_id field to a comments table, run this:
script\generate migration add_post_id_to_comment post_id:integer
See that it´s not the table name(plural), but the model name(singular),<br /> and post_id:references, does not works like in create_table.
This is the generated migration:
class AddPostIdToComment < ActiveRecord::Migration def self.up add_column :comments, :post_id, :integer end def self.down remove_column :comments, :post_id end end
![Default_avatar_30](https://www.gravatar.com/avatar/9baea8790a94beb22bb97d294d1ebbd3?default=http://apidock.com/images/default_avatar_30.png&size=30)
Options
Available options are (none of these exists by default):
* :limit - Requests a maximum column length. This is number of characters for :string and :text columns and number of bytes for :binary and :integer columns. * :default - The column‘s default value. Use nil for NULL. * :null - Allows or disallows NULL values in the column. This option could have been named :null_allowed. * :precision - Specifies the precision for a :decimal column. * :scale - Specifies the scale for a :decimal column.
![Default_avatar_30](https://www.gravatar.com/avatar/6bed507c0085d39447171b95c515a890?default=http://apidock.com/images/default_avatar_30.png&size=30)
:null => false
To not allow a column to have a NULL value, pass :null => false. Seems silly, but that’s it.
![Default_avatar_30](https://www.gravatar.com/avatar/c9c47875936bed67032a12c047b468a8?default=http://apidock.com/images/default_avatar_30.png&size=30)
![Default_avatar_30](https://www.gravatar.com/avatar/f9d47b3d6001500a435163fa341f3a4d?default=http://apidock.com/images/default_avatar_30.png&size=30)
Redirect...
See ActiveRecord::ConnectionAdapters::TableDefinition#column for details of the options you can use.
![Default_avatar_30](https://www.gravatar.com/avatar/36b4b730bc6d058fd0737fc44119ed6f?default=http://apidock.com/images/default_avatar_30.png&size=30)
script/generate can take table name
As far as I can tell script/generate will happily take the plural table name, at least in Rails 2.3.