우선 Rails프로젝트를 생성하실때 그냥 만드시면 기본은 SQLite입니다.
따라서 -d 옵션을 통해서 Mysql를 사용한다는 것을 명시해 주셔야 합니다.

  1. rails -d mysql movie_shop  

이제는 scaffold를 사용해 보겠습니다.
2.1.0 버전에서는 scaffold를 사용하실때 table의 필드들을 명령어 라인에 추가하셔야 합니다.

  1. ruby script/generate scaffold Movie title:string description:text link_url:string  

migrate를 위해서 만들어진 파일과 table을 살펴보겠습니다.
0090319021510_create_movies.rb

  1. class CreateMovies < ActiveRecord::Migration  
  2.   def self.up  
  3.     create_table :movies do |t|  
  4.       t.string :title  
  5.       t.text :description  
  6.       t.string :link_url  
  7.   
  8.       t.timestamps  
  9.     end  
  10.   end  
  11.   
  12.   def self.down  
  13.     drop_table :movies  
  14.   end  
  15. end  

t.timestamps 녀석이 기본적으로 들어가 있음을 확인하실 수 있습니다.
이녀석은 테이블에서는 created_at, updated_at으로 표현됩니다.
이제 rake명령으로 table를 생성하고
  1. rake db:migrate VERION=0090319021510_create_movies  

확인하시면 실제 만들어진 테이블은 아래와 같은 모양을 가지게 됩니다.

 

 

 

 

 

Ps)
Rails Migration Type과 MySQL Column Data Type의 상관관계를 아래와 같습니다.

 Rails Migration Type  MySql Column Data Type 

 :binary

 blob

 :boolean

 tinyint(1)

 :date

 date

 :datetime

 datetime

 :decimal

 decimal

 :float

 float

 :integer

 int(11)

 :string

 varchar(255)

 :text

 text

 :time

 time

 :timestamp

 datetime


timestamp가 초기 설정은 UTC 입니다.
config/environment.rb설정 값을 변경해 주시면 됩니다.

  1. config.time_zone = 'Seoul'  

TimeZone과 관련된 이야기를 살펴보실 수 있는 URL를 링크해 드립니다.
http://mad.ly/2008/04/09/rails-21-time-zone-support-an-overview/

Posted by 달빛변신
,