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 230 230: def initialize(base) 231: @columns = [] 232: @base = base 233: end
Instantiates a new column for the table. The type parameter must be one of the following values: :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.
Available options are (none of these exists by default):
Please be aware of different RDBMS implementations behavior with :decimal columns:
This method returns self.
# Assuming td is an instance of TableDefinition td.column(:granted, :boolean) #=> granted BOOLEAN td.column(:picture, :binary, :limit => 2.megabytes) #=> picture BLOB(2097152) td.column(:sales_stage, :string, :limit => 20, :default => 'new', :null => false) #=> sales_stage VARCHAR(20) DEFAULT 'new' NOT NULL def.column(:bill_gates_money, :decimal, :precision => 15, :scale => 2) #=> bill_gates_money DECIMAL(15,2) def.column(:sensor_reading, :decimal, :precision => 30, :scale => 20) #=> sensor_reading DECIMAL(30,20) # While <tt>:scale</tt> defaults to zero on most databases, it # probably wouldn't hurt to include it. def.column(:huge_integer, :decimal, :precision => 30) #=> huge_integer DECIMAL(30)
# File vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 319 319: def column(name, type, options = {}) 320: column = self[name] || ColumnDefinition.new(@base, name, type) 321: column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym] 322: column.precision = options[:precision] 323: column.scale = options[:scale] 324: column.default = options[:default] 325: column.null = options[:null] 326: @columns << column unless @columns.include? column 327: self 328: 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 237 237: def primary_key(name) 238: column(name, :primary_key) 239: 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 333 333: def to_sql 334: @columns * ', ' 335: end