Class | ActiveRecord::ConnectionAdapters::TableDefinition |
In: |
vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
|
Parent: | Object |
Represents a SQL table in an abstract way. Columns are stored as ColumnDefinition in the columns attribute.
columns | [RW] |
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 190 190: def initialize(base) 191: @columns = [] 192: @base = base 193: end
Returns a ColumnDefinition for the column with name name.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 202 202: def [](name) 203: @columns.find {|column| column.name.to_s == name.to_s} 204: end
Instantiates a new column for the table. The type parameter must be one of the following values: :primary_key, :string, :text, :integer, :float, :datetime, :timestamp, :time, :date, :binary, :boolean.
Available options are (none of these exists by default):
This method returns self.
# Assuming def is an instance of TableDefinition def.column(:granted, :boolean) #=> granted BOOLEAN def.column(:picture, :binary, :limit => 2.megabytes) #=> picture BLOB(2097152) def.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false) #=> sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 237 237: def column(name, type, options = {}) 238: column = self[name] || ColumnDefinition.new(@base, name, type) 239: column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym] 240: column.default = options[:default] 241: column.null = options[:null] 242: @columns << column unless @columns.include? column 243: self 244: end
Appends a primary key definition to the table definition. Can be called multiple times, but this is probably not a good idea.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 197 197: def primary_key(name) 198: column(name, native[:primary_key]) 199: end
Returns a String whose contents are the column definitions concatenated together. This string can then be pre and appended to to generate the final SQL to create the table.
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 249 249: def to_sql 250: @columns * ', ' 251: end