欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Ruby on Rails遷移時的一些注意事項

 更新時間:2015年08月04日 10:17:41   投稿:goldensun  
這篇文章主要介紹了Ruby on Rails遷移時的一些注意事項,包括建議的使用change方法取代up與down方法等細節(jié),需要的朋友可以參考下


    把 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

相關文章

最新評論