Ruby on Rails迁移时的一些注意事项
把schema.rb保存在版本管控之下。
使用rakedb:scheme:load取代rakedb:migrate来初始化空的数据库。
使用rakedb:test:prepare来更新测试数据库的schema。
避免在表里设置缺省数据。使用模型层来取代。
defamount self[:amount]or0 end
然而self[:attr_name]的使用被视为相当常见的,你也可以考虑使用更罗嗦的(争议地可读性更高的)read_attribute来取代:
defamount read_attribute(:amount)or0 end
当编写建设性的迁移时(加入表或栏位),使用Rails3.1的新方式来迁移-使用change方法取代up与down方法。
#过去的方式 classAddNameToPerson<ActiveRecord::Migration defup add_column:persons,:name,:string end defdown remove_column:person,:name end end #新的偏好方式 classAddNameToPerson<ActiveRecord::Migration defchange add_column:persons,:name,:string end end