新聞中心
下面是Yuan在學(xué)習(xí)Rails 2.3的時(shí)候總結(jié)的學(xué)習(xí)筆記,與大家分享。

Rails學(xué)習(xí)筆記1、名字:以下描述適用于rails2.1之后的版本
文件名就是個(gè)普通的ruby文件名前面加上時(shí)間戳,類(lèi)名的命名規(guī)則跟普通的ruby類(lèi)沒(méi)什么兩樣,只是要把前面的時(shí)間戳去掉,比如文件名為20080906120001_add_details_to_products.rb的migration類(lèi)名應(yīng)該是AddDetailsToProducts。
rails只把時(shí)間戳部分作為識(shí)別migration的id,所有執(zhí)行過(guò)的migration的id都將被保存到數(shù)據(jù)庫(kù)的schema_migrations表中。在rails2.1之前的版本中,migration的id是從1開(kāi)始增長(zhǎng),但是,很容易想到,在多人合作開(kāi)發(fā)項(xiàng)目的時(shí)候,這樣會(huì)很有問(wèn)題。
當(dāng)然,在新版本的rails中也可以通過(guò)在environment.rb設(shè)置config.active_record.timestamped_migrations的值為false來(lái)使用舊的方式生成migration的id。
(還是直接copy起來(lái)爽快……)
引用
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學(xué)習(xí)筆記2、修改migrations
如果寫(xiě)錯(cuò)了一個(gè)migration并且運(yùn)行過(guò),別直接修改它然后再次rake db:migrate,因?yàn)閞ails并不知道你修改了migration,執(zhí)行rake db:migrate時(shí)rails并不會(huì)做任何事。正確的做法是,先rake db:migrate:down或者rake db:rollback,然后再編輯這個(gè)migration,最后再次rake db:migrate。
一般來(lái)說(shuō)最好不要去編輯一個(gè)已經(jīng)存在了的migration,即便里面有錯(cuò)誤。
引用
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.
最好的做法是寫(xiě)一個(gè)新的migration來(lái)執(zhí)行修復(fù)上一個(gè)寫(xiě)錯(cuò)了的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學(xué)習(xí)筆記3、創(chuàng)建migrations
generate model和generate scaffold命令都會(huì)創(chuàng)建一個(gè)migration,用于生成model對(duì)應(yīng)的數(shù)據(jù)庫(kù)結(jié)構(gòu)。如果想要執(zhí)行其它的修改數(shù)據(jù)庫(kù)結(jié)構(gòu)的操作——比如說(shuō)給某個(gè)表添加一個(gè)字段,可以用generate migration來(lái)手動(dòng)創(chuàng)建一個(gè)migration。這3種創(chuàng)建migration的命令的格式都一樣:
- ruby script/generate scaffold|model|migration migration_name column_name:column_type ...
如果migration_name的名字格式是像“add_xxx_to_xxx”或者“remove_xxx_from_xxx”這樣的(經(jīng)試驗(yàn),也可以用駝峰命名法,像這樣:AddXXXToXXX),并且后面跟著字段名和類(lèi)型列表,那么rails將自動(dòng)把a(bǔ)dd_column和remove_column指令加到生成的migration代碼當(dāng)中。例如:
ruby script/generate migration add_part_number_to_products part_number:string
將會(huì)生成:
- class AddPartNumberToProducts < ActiveRecord::Migration
- def self.up
- add_column :products, :part_number, :string
- end
- def self.down
- remove_column :products, :part_number
- end
- end
這樣的ruby代碼。
Rails學(xué)習(xí)筆記4、writing a migration
建表
- create_table :products do |t|
- t.string :name
- end
以上代碼將會(huì)創(chuàng)建一個(gè)名為products的表,其中包含一個(gè)名為name的字符類(lèi)型字段。還有一種建表的方式是用bloc參數(shù)t的column方法,像這樣:Ruby代碼
- create_table :products do |t|
- t.column :name, :string
- end
引用
the first form(原文是second,我把代碼的順序調(diào)換了一下), 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方法默認(rèn)會(huì)創(chuàng)建一個(gè)名為id的整型自增長(zhǎng)字段作為主鍵(我還是比較喜歡uuid,google了一下,要使用uuid作為主鍵可參考:http://iceskysl.1sters.com/?action=show&id=349)。如果不想使用默認(rèn)的id作為主鍵名稱(chēng),可以使用:primary_key來(lái)指定某一個(gè)字段為主鍵。如果要?jiǎng)?chuàng)建一個(gè)不帶主鍵的表(比如說(shuō)一個(gè)多對(duì)多關(guān)聯(lián)的“中間表”就不需要主鍵),可以傳遞一個(gè)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.
修改表結(jié)構(gòu)
- change_table :products do |t|
- t.remove :description, :name
- t.string :part_number
- t.index :part_number
- t.rename :upccode, :upc_code
- end
以上代碼修改了products表結(jié)構(gòu),刪除了其中的description和name字段,添加了一個(gè)字符類(lèi)型的part_number字段,并且給part_number字段添加索引,最后把upccode字段改名為upc_code。上面的代碼完成的事和以下代碼是一樣的:
- remove_column :products, :description
- remove_column :products, :name
- add_column :products, :part_number, :string
- add_index :products, :part_number
- 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.
【相關(guān)閱讀】
- Ruby on Rails 2.3.4發(fā)布 安全級(jí)別:重要
- Ruby on Rails開(kāi)發(fā)的五點(diǎn)建議
- Ruby的瓶頸 以及PHP何以成為Web之王
- 淺談Ruby和JRuby的學(xué)習(xí)
- Web開(kāi)發(fā)誰(shuí)更高效 Java對(duì)決Ruby on Rails
分享文章:Rails學(xué)習(xí)筆記:名字與migrations
當(dāng)前鏈接:http://m.5511xx.com/article/dpjgehj.html


咨詢(xún)
建站咨詢(xún)
