Ruby on Rails遷移時的一些注意事項
把 schema.rb 保存在版本管控之下。
使用 rake db:scheme:load 取代 rake db:migrate 來初始化空的數(shù)據(jù)庫。
使用 rake db:test:prepare 來更新測試數(shù)據(jù)庫的 schema。
避免在表里設置缺省數(shù)據(jù)。使用模型層來取代。
def amount self[:amount] or 0 end
然而 self[:attr_name] 的使用被視為相當常見的,你也可以考慮使用更羅嗦的(爭議地可讀性更高的) read_attribute 來取代:
def amount read_attribute(:amount) or 0 end
當編寫建設性的遷移時(加入表或欄位),使用 Rails 3.1 的新方式來遷移 - 使用 change 方法取代 up 與 down 方法。
# 過去的方式 class AddNameToPerson < ActiveRecord::Migration def up add_column :persons, :name, :string end def down remove_column :person, :name end end # 新的偏好方式 class AddNameToPerson < ActiveRecord::Migration def change add_column :persons, :name, :string end end
相關文章
ruby、javascript、php中的觀察者模式實現(xiàn)代碼
這篇文章主要介紹了ruby、javascript、php中的觀察者模式實現(xiàn)代碼,通過本文可以深入的理解觀察者模式,需要的朋友可以參考下2014-11-11Ruby中的Proc類及Proc的類方法Proc.new的使用解析
用Proc類可以用Proc.new來創(chuàng)建一個Proc類,進而來操作塊,這里我們就來進行Ruby中的Proc類及Proc的類方法Proc.new的使用解析.2016-05-05Ruby簡潔學習筆記(一):字符串、數(shù)字、類和對象
這篇文章主要介紹了Ruby簡潔學習筆記(一):字符串、數(shù)字、類和對象,本文是學習筆記第一篇,需要的朋友可以參考下2015-01-01