自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Rails學習筆記:名字與migrations

開發(fā) 開發(fā)工具
本文是一篇Rails學習筆記,里面對名字以及migrations的修改和創(chuàng)建進行了簡單的回顧。

下面是Yuan在學習Rails 2.3的時候總結的學習筆記,與大家分享。

Rails學習筆記1、名字:以下描述適用于rails2.1之后的版本

文件名就是個普通的ruby文件名前面加上時間戳,類名的命名規(guī)則跟普通的ruby類沒什么兩樣,只是要把前面的時間戳去掉,比如文件名為20080906120001_add_details_to_products.rb的migration類名應該是AddDetailsToProducts。

rails只把時間戳部分作為識別migration的id,所有執(zhí)行過的migration的id都將被保存到數(shù)據(jù)庫的schema_migrations表中。在rails2.1之前的版本中,migration的id是從1開始增長,但是,很容易想到,在多人合作開發(fā)項目的時候,這樣會很有問題。

當然,在新版本的rails中也可以通過在environment.rb設置config.active_record.timestamped_migrations的值為false來使用舊的方式生成migration的id。

(還是直接copy起來爽快……)

引用

The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.

For example Alice adds migrations 20080906120000 and 20080906123000 and Bob adds 20080906124500 and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice’s two migrations so rake db:migrate would run them (even though Bob’s migration with a later timestamp has been run), and similarly migrating down would not run their down methods.

Rails學習筆記2、修改migrations

如果寫錯了一個migration并且運行過,別直接修改它然后再次rake db:migrate,因為rails并不知道你修改了migration,執(zhí)行rake db:migrate時rails并不會做任何事。正確的做法是,先rake db:migrate:down或者rake db:rollback,然后再編輯這個migration,最后再次rake db:migrate。

一般來說最好不要去編輯一個已經(jīng)存在了的migration,即便里面有錯誤。

引用

you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines.

最好的做法是寫一個新的migration來執(zhí)行修復上一個寫錯了的migration的操作。

引用

Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.

Rails學習筆記3、創(chuàng)建migrations

generate model和generate scaffold命令都會創(chuàng)建一個migration,用于生成model對應的數(shù)據(jù)庫結構。如果想要執(zhí)行其它的修改數(shù)據(jù)庫結構的操作——比如說給某個表添加一個字段,可以用generate migration來手動創(chuàng)建一個migration。這3種創(chuàng)建migration的命令的格式都一樣:

  1. ruby script/generate scaffold|model|migration migration_name column_name:column_type ... 

如果migration_name的名字格式是像“add_xxx_to_xxx”或者“remove_xxx_from_xxx”這樣的(經(jīng)試驗,也可以用駝峰命名法,像這樣:AddXXXToXXX),并且后面跟著字段名和類型列表,那么rails將自動把add_column和remove_column指令加到生成的migration代碼當中。例如:

ruby script/generate migration add_part_number_to_products part_number:string

將會生成:

  1. class AddPartNumberToProducts < ActiveRecord::Migration  
  2.  def self.up  
  3.   add_column :products:part_number:string 
  4.  end 
  5.  def self.down  
  6.   remove_column :products:part_number 
  7.  end 
  8. end  

這樣的ruby代碼。

Rails學習筆記4、writing a migration

建表

  1. create_table :products do |t|  
  2.   t.string :name 
  3. end   

以上代碼將會創(chuàng)建一個名為products的表,其中包含一個名為name的字符類型字段。還有一種建表的方式是用bloc參數(shù)t的column方法,像這樣:Ruby代碼

  1. create_table :products do |t|  
  2.   t.column :name:string 
  3. end   
  4.  

引用

the first form(原文是second,我把代碼的順序調換了一下), the so called “sexy” migration, drops the somewhat redundant column method. Instead, the string, integer, etc. methods create a column of that type. Subsequent parameters are the same.

create_table方法默認會創(chuàng)建一個名為id的整型自增長字段作為主鍵(我還是比較喜歡uuid,google了一下,要使用uuid作為主鍵可參考:http://iceskysl.1sters.com/?action=show&id=349)。如果不想使用默認的id作為主鍵名稱,可以使用:primary_key來指定某一個字段為主鍵。如果要創(chuàng)建一個不帶主鍵的表(比如說一個多對多關聯(lián)的“中間表”就不需要主鍵),可以傳遞一個hash :id=>false給create_table方法。

引用

The types supported by Active Record are :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.

修改表結構

  1. change_table :products do |t|  
  2.  t.remove :description:name 
  3.  t.string :part_number 
  4.  t.index :part_number 
  5.  t.rename :upccode:upc_code 
  6. end   

以上代碼修改了products表結構,刪除了其中的description和name字段,添加了一個字符類型的part_number字段,并且給part_number字段添加索引,最后把upccode字段改名為upc_code。上面的代碼完成的事和以下代碼是一樣的:

  1. remove_column :products:description 
  2. remove_column :products:name 
  3. add_column :products:part_number:string 
  4. add_index :products:part_number 
  5. rename_column :products:upccode:upc_code   

引用

You don’t have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example remove_column becomes just remove and add_index becomes just index.

【相關閱讀】

  1. Ruby on Rails 2.3.4發(fā)布 安全級別:重要
  2. Ruby on Rails開發(fā)的五點建議
  3. Ruby的瓶頸 以及PHP何以成為Web之王
  4. 淺談Ruby和JRuby的學習
  5. Web開發(fā)誰更高效 Java對決Ruby on Rails
責任編輯:yangsai 來源: JavaEye博客
相關推薦

2009-10-13 14:29:49

VB.NET名字空間

2009-07-14 17:01:24

JDBC基礎

2009-09-06 14:55:38

CCNA學習筆記

2010-07-06 10:56:32

UML圖詳解

2011-03-08 16:30:30

Proftpd命令Proftpd配置

2010-06-01 19:33:53

SVN與CVS優(yōu)缺點

2010-03-25 14:43:14

云計算

2010-05-28 17:15:17

SVN分支與合并

2010-06-18 09:29:37

UML與Rationa

2009-08-07 10:27:45

Eclipse和Net

2009-08-27 10:21:22

Ruby on Rai

2010-05-31 10:47:08

WindowsSVN服

2011-08-30 16:43:46

MTK開發(fā)菜單

2010-07-21 13:53:07

Perl引用

2010-09-07 14:44:50

DB2 數(shù)據(jù)庫

2020-12-07 19:00:29

Rails

2020-10-28 21:00:38

RailsMVC命令

2011-03-17 16:43:49

2009-07-14 16:08:41

WebWork學習

2010-08-10 09:40:23

Flex與瀏覽器交互
點贊
收藏

51CTO技術棧公眾號