把 schema.rb 保存在版本管控之下。
使用 rake db:scheme:load 取代 rake db:migrate 來初始化空的數據庫。
使用 rake db:test:prepare 來更新測試數據庫的 schema。
避免在表里設置缺省數據。使用模型層來取代。
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
新聞熱點
疑難解答
圖片精選