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

Ruby on Rails遷移時(shí)的一些注意事項(xiàng)

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


    把 schema.rb 保存在版本管控之下。
    使用 rake db:scheme:load 取代 rake db:migrate 來(lái)初始化空的數(shù)據(jù)庫(kù)。
    使用 rake db:test:prepare 來(lái)更新測(cè)試數(shù)據(jù)庫(kù)的 schema。

    避免在表里設(shè)置缺省數(shù)據(jù)。使用模型層來(lái)取代。

  def amount
   self[:amount] or 0
  end

    然而 self[:attr_name] 的使用被視為相當(dāng)常見的,你也可以考慮使用更羅嗦的(爭(zhēng)議地可讀性更高的) read_attribute 來(lái)取代:

    

def amount
   read_attribute(:amount) or 0
  end

    當(dāng)編寫建設(shè)性的遷移時(shí)(加入表或欄位),使用 Rails 3.1 的新方式來(lái)遷移 - 使用 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

相關(guān)文章

最新評(píng)論