当前位置: 移动技术网 > IT编程>脚本编程>Ruby > Ruby on Rails迁移时的一些注意事项

Ruby on Rails迁移时的一些注意事项

2017年12月08日  | 移动技术网IT编程  | 我要评论


    把 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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网