Rails 마이그레이션 중 NOT NULL, DEFAULT 등등 설정하는 방법

  db/migrate/20140910171610_create_my.rb에 아래와 같이 생성된 마이그레이션 파일이 있다고 칩시다.
class CreateVideos < ActiveRecord::Migration
  def change
    create_table :videos do |t|
      t.string :url
      t.text :desc
      t.integer :views
      t.integer :recommand
      t.datetime :up_date
      t.datetime :mod_date
 
      t.timestamps
    end
  end
end


  url은 당연히 비어 있으면 안 되고, desc는 기본적으로 '설명이 없습니다.'를 표시하게 하고 싶습니다. 또, views와 recommand는 기본적으로 0이란 값을, up_date는 현재 날짜와 시각 정보를 자동으로 입력하고 싶은데 어떻게 해야 할까요?

  정답은 상황에 맞는 심볼을 조작하면 됩니다.
class CreateVideos < ActiveRecord::Migration
  def change
    create_table :videos do |t|
      t.string :url, :null => false
      t.text :desc, :default => '설명이 없습니다.'
      t.integer :views, :default => 0
      t.integer :recommand, :default => 0
      t.datetime :up_date, :default => Time.now()
      t.datetime :mod_date
 
      t.timestamps
    end
  end
end

댓글

이 블로그의 인기 게시물

C# 남아도는 메모리에도 불구하고 OutOfMemoryException이 발생한다면?

MySQL 데이터 타입과 Java 데이터 타입 비교/매칭

테일즈위버 OST 전곡 모음