無設密碼且是預設使用者root: mysqladmin -u root create first( datebase name)

有設密碼且是預設使用者root: mysqladmin -u root -p create first

配置資訊在config/database.yml中


first:
  adapter: mysql   (使用何種資料庫)
  database: first    (資料庫名稱)
  username: root  (使用者名稱)
  password:           (使用者密碼)
  host: localhost    (資料庫在何種機器上,預設為localhost (自己主機) )

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
  adapter: mysql
  database: first
  username: root
  password:
  host: localhost

production:
  adapter: mysql
  database: first
  username: root
  password:
  host: localhost

(以上為mysql版本參數)

 


 

測試配置:

rake db:migrate

看rails是否可聯結資料庫

回傳 in /home/user/桌面/testing

表示正確

 


 

建立產品模型和資料表:

script/generate model product

rails中model會自動映設到資料庫中的資料表(table)

資料表名稱就是模型類別的複數形式

此時會產生遷移檔

001_create_products.rb ( 001: 序號首碼   名稱   副檔名 )

編輯此檔案使之產生table (也可以直接進入mysql產生)

class CreateProducts < ActiveRecord::Migration
  def self.up
    create_table :products do |t|
    t.column :title, :string
    t.column :description, :text
    t.column :image_url, :string
    end
  end

  def self.down
    drop_table :products
  end
end

這時

再輸入 rake db:migrate (資料庫有做變更時使用)

便可建立好資料表

文章標籤
全站熱搜
創作者介紹
創作者 flyinsky76 的頭像
flyinsky76

Deja Vu

flyinsky76 發表在 痞客邦 留言(0) 人氣(134)