close

這裡主要是講 database 使用 mysql 的狀況。

在 Rails 裡,對於 text 的指定只有一種。

而有時我們需要更大的欄位,比方說 medium text, big text。因為有時候,大一點還是比較好。

這時候在 migration 裡的作法有兩種個方式:

第一種是以指定 limit 大小的方式讓 mysql 自行將欄位設定為 medium text (請注意 "content" ):

  1. class CreateArticles < ActiveRecord::Migration
  2.   def self.up
  3.     create_table :articles do |t|
  4.       t.string    :title
  5.       t.text      :content, :limit => 64.kilobytes + 1
  6.       t.timestamps
  7.     end
  8.   end
  9.  
  10.   def self.down
  11.     drop_table :articles
  12.   end
  13. end
  14.  

第二種是在 self.up 後再 exeucte 自行生成你要的欄位:

  1. class CreateArticles < ActiveRecord::Migration
  2.   def self.up
  3.     create_table :articles do |t|
  4.       t.string    :title
  5.       t.timestamps
  6.     end
  7.     execute "ALTER TABLE articles ADD `content` MEDIUMTEXT NOT NULL AFTER `title`"
  8.   end
  9.  
  10.   def self.down
  11.     drop_table :articles
  12.   end
  13. end
arrow
arrow
    全站熱搜

    沒力小僧 發表在 痞客邦 留言(0) 人氣()