close
這裡主要是講 database 使用 mysql 的狀況。
在 Rails 裡,對於 text 的指定只有一種。
而有時我們需要更大的欄位,比方說 medium text, big text。因為有時候,大一點還是比較好。
這時候在 migration 裡的作法有兩種個方式:
第一種是以指定 limit 大小的方式讓 mysql 自行將欄位設定為 medium text (請注意 "content" ):
-
class CreateArticles < ActiveRecord::Migration
-
def self.up
-
create_table :articles do |t|
-
t.string :title
-
t.text :content, :limit => 64.kilobytes + 1
-
t.timestamps
-
end
-
end
-
-
def self.down
-
drop_table :articles
-
end
-
end
-
第二種是在 self.up 後再 exeucte 自行生成你要的欄位:
-
class CreateArticles < ActiveRecord::Migration
-
def self.up
-
create_table :articles do |t|
-
t.string :title
-
t.timestamps
-
end
-
execute "ALTER TABLE articles ADD `content` MEDIUMTEXT NOT NULL AFTER `title`"
-
end
-
-
def self.down
-
drop_table :articles
-
end
-
end
全站熱搜