class ActiveRecord::ConnectionAdapters::TableDefinition
def column(name, type, index: nil, **options)
t.references :taggable, polymorphic: { default: 'Photo' }, index: false
t.references :tagger, polymorphic: true
t.references :tag, index: { name: 'index_taggings_on_tag_id' }
create_table :taggings do |t|
Can also be written as follows using references:
add_index :taggings, [:tagger_id, :tagger_type]
add_index :taggings, :tag_id, name: 'index_taggings_on_tag_id'
end
t.string :taggable_type, default: 'Photo'
t.string :tagger_type
t.integer :tag_id, :tagger_id, :taggable_id
create_table :taggings do |t|
So what can be written like this:
will also create an index, similar to calling {add_index}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_index].
options, these will be used when creating the _type column. The :index option
column if the :polymorphic option is supplied. If :polymorphic is a hash of
TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type
TableDefinition#timestamps that'll add +created_at+ and +updated_at+ as datetimes.
There's a short-hand method for each of the type values declared at the top. And then there's
end
t.timestamps null: false
t.string :name, :value, default: "Untitled"
t.string :item_number, index: true
t.integer :shop_id, :creator_id
create_table :products do |t|
can also be written as follows using the short-hand:
add_index :products, :item_number
end
t.column :updated_at, :datetime
t.column :created_at, :datetime
t.column :value, :string, default: "Untitled"
t.column :name, :string, default: "Untitled"
t.column :item_number, :string
t.column :creator_id, :integer
t.column :shop_id, :integer
create_table :products do |t|
What can be written like this with the regular calls to column:
in a single statement.
They use the type as the method name instead of as a parameter and allow for multiple columns to be defined
Instead of calling #column directly, you can also work with the short-hand definitions for the default types.
== Short-hand examples
td.column(:granted, :boolean, index: true)
# Assuming +td+ is an instance of TableDefinition
== Examples
This method returns self.
Create an index for the column. Can be either true or an options hash.
* :index -
Additional options are:
for available options.
See {connection.add_column}[rdoc-ref:ConnectionAdapters::SchemaStatements#add_column]
Instantiates a new column for the table.
def column(name, type, index: nil, **options) name = name.to_s type = type.to_sym if type if @columns_hash[name] if @columns_hash[name].primary_key? raise ArgumentError, "you can't redefine the primary key column '#{name}'. To define a custom primary key, pass { id: false } to create_table." else raise ArgumentError, "you can't define an already defined column '#{name}'." end end if @conn.supports_datetime_with_precision? if type == :datetime && !options.key?(:precision) options[:precision] = 6 end end @columns_hash[name] = new_column_definition(name, type, **options) if index index_options = index.is_a?(Hash) ? index : {} index(name, **index_options) end self end