Rails学习笔记:名字与migrations

开发 开发工具
本文是一篇Rails学习笔记,里面对名字以及migrations的修改和创建进行了简单的回顾。

下面是Yuan在学习Rails 2.3的时候总结的学习笔记,与大家分享。

Rails学习笔记1、名字:以下描述适用于rails2.1之后的版本

文件名就是个普通的ruby文件名前面加上时间戳,类名的命名规则跟普通的ruby类没什么两样,只是要把前面的时间戳去掉,比如文件名为20080906120001_add_details_to_products.rb的migration类名应该是AddDetailsToProducts。

rails只把时间戳部分作为识别migration的id,所有执行过的migration的id都将被保存到数据库的schema_migrations表中。在rails2.1之前的版本中,migration的id是从1开始增长,但是,很容易想到,在多人合作开发项目的时候,这样会很有问题。

当然,在新版本的rails中也可以通过在environment.rb设置config.active_record.timestamped_migrations的值为false来使用旧的方式生成migration的id。

(还是直接copy起来爽快……)

引用

The combination of timestamps and recording which migrations have been run allows Rails to handle common situations that occur with multiple developers.

For example Alice adds migrations 20080906120000 and 20080906123000 and Bob adds 20080906124500 and runs it. Alice finishes her changes and checks in her migrations and Bob pulls down the latest changes. Rails knows that it has not run Alice’s two migrations so rake db:migrate would run them (even though Bob’s migration with a later timestamp has been run), and similarly migrating down would not run their down methods.

Rails学习笔记2、修改migrations

如果写错了一个migration并且运行过,别直接修改它然后再次rake db:migrate,因为rails并不知道你修改了migration,执行rake db:migrate时rails并不会做任何事。正确的做法是,先rake db:migrate:down或者rake db:rollback,然后再编辑这个migration,最后再次rake db:migrate。

一般来说最好不要去编辑一个已经存在了的migration,即便里面有错误。

引用

you will be creating extra work for yourself and your co-workers and cause major headaches if the existing version of the migration has already been run on production machines.

最好的做法是写一个新的migration来执行修复上一个写错了的migration的操作。

引用

Editing a freshly generated migration that has not yet been committed to source control (or more generally which has not been propagated beyond your development machine) is relatively harmless. Just use some common sense.

Rails学习笔记3、创建migrations

generate model和generate scaffold命令都会创建一个migration,用于生成model对应的数据库结构。如果想要执行其它的修改数据库结构的操作——比如说给某个表添加一个字段,可以用generate migration来手动创建一个migration。这3种创建migration的命令的格式都一样:

  1. ruby script/generate scaffold|model|migration migration_name column_name:column_type ... 

如果migration_name的名字格式是像“add_xxx_to_xxx”或者“remove_xxx_from_xxx”这样的(经试验,也可以用驼峰命名法,像这样:AddXXXToXXX),并且后面跟着字段名和类型列表,那么rails将自动把add_column和remove_column指令加到生成的migration代码当中。例如:

ruby script/generate migration add_part_number_to_products part_number:string

将会生成:

  1. class AddPartNumberToProducts < ActiveRecord::Migration  
  2.  def self.up  
  3.   add_column :products:part_number:string 
  4.  end 
  5.  def self.down  
  6.   remove_column :products:part_number 
  7.  end 
  8. end  

这样的ruby代码。

Rails学习笔记4、writing a migration

建表

  1. create_table :products do |t|  
  2.   t.string :name 
  3. end   

以上代码将会创建一个名为products的表,其中包含一个名为name的字符类型字段。还有一种建表的方式是用bloc参数t的column方法,像这样:Ruby代码

  1. create_table :products do |t|  
  2.   t.column :name:string 
  3. end   
  4.  

引用

the first form(原文是second,我把代码的顺序调换了一下), the so called “sexy” migration, drops the somewhat redundant column method. Instead, the string, integer, etc. methods create a column of that type. Subsequent parameters are the same.

create_table方法默认会创建一个名为id的整型自增长字段作为主键(我还是比较喜欢uuid,google了一下,要使用uuid作为主键可参考:http://iceskysl.1sters.com/?action=show&id=349)。如果不想使用默认的id作为主键名称,可以使用:primary_key来指定某一个字段为主键。如果要创建一个不带主键的表(比如说一个多对多关联的“中间表”就不需要主键),可以传递一个hash :id=>false给create_table方法。

引用

The types supported by Active Record are :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.

修改表结构

  1. change_table :products do |t|  
  2.  t.remove :description:name 
  3.  t.string :part_number 
  4.  t.index :part_number 
  5.  t.rename :upccode:upc_code 
  6. end   

以上代码修改了products表结构,删除了其中的description和name字段,添加了一个字符类型的part_number字段,并且给part_number字段添加索引,最后把upccode字段改名为upc_code。上面的代码完成的事和以下代码是一样的:

  1. remove_column :products:description 
  2. remove_column :products:name 
  3. add_column :products:part_number:string 
  4. add_index :products:part_number 
  5. rename_column :products:upccode:upc_code   

引用

You don’t have to keep repeating the table name and it groups all the statements related to modifying one particular table. The individual transformation names are also shorter, for example remove_column becomes just remove and add_index becomes just index.

【相关阅读】

  1. Ruby on Rails 2.3.4发布 安全级别:重要
  2. Ruby on Rails开发的五点建议
  3. Ruby的瓶颈 以及PHP何以成为Web之王
  4. 浅谈Ruby和JRuby的学习
  5. Web开发谁更高效 Java对决Ruby on Rails
责任编辑:yangsai 来源: JavaEye博客
相关推荐

2009-10-13 14:29:49

VB.NET名字空间

2009-09-06 14:55:38

CCNA学习笔记

2010-07-06 10:56:32

UML图详解

2009-07-14 17:01:24

JDBC基础

2011-03-08 16:30:30

Proftpd命令Proftpd配置

2009-08-07 10:27:45

Eclipse和Net

2010-06-01 19:33:53

SVN与CVS优缺点

2010-03-25 14:43:14

云计算

2010-05-28 17:15:17

SVN分支与合并

2010-06-18 09:29:37

UML与Rationa

2009-08-27 10:21:22

Ruby on Rai

2010-09-07 14:44:50

DB2 数据库

2020-12-07 19:00:29

Rails

2020-10-28 21:00:38

RailsMVC命令

2010-05-31 10:47:08

WindowsSVN服

2010-07-21 13:53:07

Perl引用

2011-08-30 16:43:46

MTK开发菜单

2017-10-09 08:45:13

编程语言Amazon AtheSharePoint

2009-08-06 09:13:36

Ruby on Rai

2010-07-12 10:43:41

UML交互图
点赞
收藏

51CTO技术栈公众号