下面是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的命令的格式都一样:
- 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
将会生成:
- class AddPartNumberToProducts < ActiveRecord::Migration
- def self.up
- add_column :products, :part_number, :string
- end
- def self.down
- remove_column :products, :part_number
- end
- end
这样的ruby代码。
Rails学习笔记4、writing a migration
建表
- create_table :products do |t|
- t.string :name
- end
以上代码将会创建一个名为products的表,其中包含一个名为name的字符类型字段。还有一种建表的方式是用bloc参数t的column方法,像这样:Ruby代码
- create_table :products do |t|
- t.column :name, :string
- end
引用
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.
修改表结构
- change_table :products do |t|
- t.remove :description, :name
- t.string :part_number
- t.index :part_number
- t.rename :upccode, :upc_code
- end
以上代码修改了products表结构,删除了其中的description和name字段,添加了一个字符类型的part_number字段,并且给part_number字段添加索引,最后把upccode字段改名为upc_code。上面的代码完成的事和以下代码是一样的:
- remove_column :products, :description
- remove_column :products, :name
- add_column :products, :part_number, :string
- add_index :products, :part_number
- 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.
【相关阅读】
当前标题:Rails学习笔记:名字与migrations
文章路径:http://www.shufengxianlan.com/qtweb/news8/315008.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联