RUBY AND RAILS COMMAND

Rails2.0 命令大全
2008-05-21 22:11
一.铁道
1.1 创建一个Rails应用程序
$ rails app_name
可选项:
-d, database=xxx 指定安装一个数据库(mysql oracle postgresql sqlite2 sqlite3 ), 默认情况下是数据库
-r, ruby-path= 指定Ruby的安装路径,如果没有指定,scripts使用env去找Ruby
-f, freeze (冻结)freezes Rails在vendor/rails目录

1.2 API Documentation
$ gem_server
启动一个WEBrick服务器。这时候你可以通过Http://localhost:8808/ 打开浏览器去查看rails API文档

1.3 Rake
rake db:fixtures:load
   # 载入fixtures到当前环境的数据库
   # 载入指定的fixtures使用FIXTURES=x,y
rake db:migrate
# 迁移数据库通过在db/migrate目录下的脚本.可以指定版本号通过VERSION=x
rake db:schema:dump
# 创建一个db/schema.rb文件,通过AR能过够支持任何数据库去使用
rake db:schema:load
# 再入一个schema.rb文件进数据库
rake db:sessions:clear
# 清空sessions表
rake db:sessions:create
# 用CGI::Session::ActiveRecordStore创建一个sessions表为用户
rake db:structure:dump
# 导出数据库结构为一个SQL文件
rake db:test:clone
# 重新创建一个测试数据库从当前环境数据库中
rake db:test:clone_structure
# 重新创建测试数据库从开发模式数据库
rake db:test:prepare
# 准备测试数据库并在入schema
rake db:test:purge
# 清空测试数据库
rake doc:app
# 创建HTML文件的API Documentation
rake doc:clobber_app
# 删除Documentation
rake doc:clobber_plugins
# 删除 plugin Documentation
rake doc:clobber_rails
# 删除Documentation
rake doc:plugins
# 产生Documation为所有安装的plugins
rake doc:rails
# 创建HTML文件的API Documentation
rake doc:reapp
# 强制重新创建HTML文件的API Documentation
rake doc:rerails
# 强制重新创建HTML文件的API Documentation
rake log:clear
# 清空目录log/下的所有日志文件
rake rails:freeze:edge
# Lock this application to latest Edge Rails. Lock a specific revision with REVISION=X
rake rails:freeze:gems
# Lock this application to the current gems (by unpacking them into vendor/rails)
rake rails:unfreeze
# Unlock this application from freeze of gems or edge and return to a fluid use of system gems
rake rails:update
# Update both scripts and public/javascripts from Rails
rake rails:update:javascripts
# Update your javascripts from your current rails install
rake rails:update:scripts
# Add new scripts to the application script/ directory
rake stats
# Report code statistics (KLOCs, etc) from the application
rake test
# Test all units and functionals
rake test:functionals
# Run tests for functionalsdb:test:prepare
rake test:integration
# Run tests for integrationdb:test:prepare
rake test:plugins
# Run tests for pluginsenvironment
rake test:recent
# Run tests for recentdb:test:prepare
rake test:uncommitted
# Run tests for uncommitteddb:test:prepare
rake test:units
# Run tests for unitsdb:test:prepare
rake tmp:cache:clear
# 清空tmp/cache目录下的所有文件
rake tmp:clear
# 清空session, cache, 和socket文件从tmp/目录
rake tmp:create
# 为sessions, cache, and sockets创建tmp/目录
rake tmp:sessions:clear
# 清空所有在tmp/sessions目录下的文件
rake tmp:sockets:clear
# 清空所有在tmp/sessions 目录下的ruby_sess.* 文件

1.4 Scripts
script/about
# 输出当前环境信息
script/breakpointer
# 启动断点server
script/console
# 启动交换式的Rails控制台
script/destroy
# 删除通过generators创建的文件
script/generate
# -> generators
script/plugin
# -> Plugins
script/runner
# 执行一个任务在rails上下文中
script/server
# 启动开发模式服务器http://localhost:3000
//以下几个不知道怎么去使用
script/performance/profiler
script/performance/benchmarker
script/process/reaper
script/process/spawner

1.5 Generators
ruby script/generate model ModelName
ruby script/generate controller ListController show edit
ruby script/generate scaffold ModelName ControllerName
ruby script/generate migration AddNewTable
ruby script/generate plugin PluginName
ruby script/generate mailer Notification lost_password signup
ruby script/generate web_service ServiceName api_one api_two
ruby script/generate integration_test TestName
ruby script/generate session_migration
可选项:
-p, --pretend Run but do not make any changes.
-f, --force Overwrite files that already exist.
-s, --skip Skip files that already exist.
-q, --quiet Suppress normal output.
-t, --backtrace Debugging: show backtrace on errors.
-h, --help Show this help message.
-c, --svn Modify files with subversion. (Note: svn must be in path)

1.6 Plugins
script/plugin discover
# discover plugin repositories
script/plugin list
# list all available plugins
script/plugin install where
# install the a wherea ? plugin
script/plugin install -x where
# install where plugin as SVN external
script/plugin install http://invisible.ch/projects/plugins/where
script/plugin update
# update installed plugins
script/plugin source
# add a source repository
script/plugin unsource
# removes a source repository
script/plugin sources
# lists source repositories

诗歌rails之 Migrations

 

对于Rails的迁移功能Migrations,一直都只是看一下网上的一些很基础很基础的代码片断就开始动手写代码,对它的认识基本上就是停留在抄袭的层面,连会用都说不上.有感于此,终下决心要弄清楚Migrations,至少得会用啊,山寨抄袭终非王道.

学习Migrations最佳的学习资料莫过于 Ruby On Rails网站上的 Guides 系统文章了,链接在 http://guides.rubyonrails.org/migrations.html
本文的很多代码都是出自那里.

在我的理解中,Migrations就是一个基于ruby,针对数据库(SQL)的DSL,它的出现也是符合Rails中处处皆Ruby的原则的,正是专注于Ruby,这样Rails才显得别样的美丽.

=========================== 如何写migration =========================


1.migration的结构

每一个migrate的类都是 ActiveRecord::Migration 的子类,每一个migrate都要重写两个方法 up 和 down:
Ruby代码
  1. class  CreateProducts < ActiveRecord::Migration   
  2.   def   self .up   
  3.      #想干嘛,就干嘛   
  4.    end     
  5.    def   self .down   
  6.       #你后悔的时候,你会怎么做?    
  7.     end    
  8. end    
class CreateProducts < ActiveRecord::Migration 
  def self.up 
     #想干嘛,就干嘛
   end  
   def self.down 
      #你后悔的时候,你会怎么做? 
    end 
end 

简单的说 up 方法就是操作数据库时用的,down就是你后悔了,用来回滚用的.


2.migration提供调用的方法

Migrations提供了一系列的方法来操作数据库:
Ruby代码
  1. create_table   #建表   
  2. change_table  #修改表结构   
  3. drop_table    #删除表   
  4. add_column    #增加字段   
  5. change_column #修改字段定义   
  6. rename_column #修改字段名   
  7. remove_column #删除字段   
  8. add_index     #创建索引   
  9. remove_index  #删除索引   
     create_table  #建表
     change_table  #修改表结构
     drop_table    #删除表
     add_column    #增加字段
     change_column #修改字段定义
     rename_column #修改字段名
     remove_column #删除字段
     add_index     #创建索引
     remove_index  #删除索引

具体各个方法的详细定义,可以查看Rails的API http://api.rubyonrails.org/classes/ActiveRecord/Migration.html
这些方法替代使用SQL来操作数据库,当然也可以使用 execute 方法直接使用 SQL 来操作数据库,个人不推荐这种方式,但是在某些情况下,提供直接使用SQL也是很方便的:
Ruby代码
  1. execute <<-SQL   
  2. ALTER TABLE products ADD CONSTRAINT fk_products_categories FOREIGN KEY (category_id)  REFERENCES categories(id)    
  3. SQL   
execute <<-SQL 
ALTER TABLE products ADD CONSTRAINT fk_products_categories FOREIGN KEY (category_id)  REFERENCES categories(id)  
SQL 



3.字段

定义字段有两种方法:
Ruby代码
  1. #这个是传统的方法   
  2. create_table :products   do  |t|   
  3. t.column :name:string:null  =>  false    
  4. end    
  5.   
  6. #这个刚出来的时候,大家都说很性感,其实不外乎就是定义一系列的快捷方法: string,   
  7. # text, integer, float, decimal, datetime, timestamp, time, date,   
  8. # binary, boolean . 这一系列的方法对应各种数据类型,   
  9. # 还有 primary_key 方法是用来定义主键的.   
  10. create_table :products   do  |t|   
  11. t.string :name    
  12. end    
#这个是传统的方法
create_table :products do |t| 
t.column :name, :string, :null => false 
end 

#这个刚出来的时候,大家都说很性感,其实不外乎就是定义一系列的快捷方法: string,
# text, integer, float, decimal, datetime, timestamp, time, date,
# binary, boolean . 这一系列的方法对应各种数据类型,
# 还有 primary_key 方法是用来定义主键的.
create_table :products do |t| 
t.string :name 
end 

除了这几个经典的定义字段方法外,还有两个特别的Helper方法:
Ruby代码
  1. #以下这个方法会自动在表中增加 created_at,updated_at这两个类型为timestamp的字段   
  2. change_table :products   do  |t|   
  3. t.timestamps   
  4. end    
  5.   
  6. #这个方法是定义关于关系的,但是不会为你的表加上外键约束,如果你加上约束,请另外手动添加,切记!   
  7. create_table :products   do  |t|   
  8. t.references :category   # 生成 category_id   
  9.   
  10. #这个是关联关系中多态的定义,生成两个字段 attachment_id 和 attachment_type ,并且attachment_type的默认值为 'Photo'   
  11. t.references :attachment:polymorphic  => { :default  =>  'Photo' }   
  12. end    
#以下这个方法会自动在表中增加 created_at,updated_at这两个类型为timestamp的字段
change_table :products do |t| 
t.timestamps 
end 

#这个方法是定义关于关系的,但是不会为你的表加上外键约束,如果你加上约束,请另外手动添加,切记!
create_table :products do |t| 
t.references :category # 生成 category_id

#这个是关联关系中多态的定义,生成两个字段 attachment_id 和 attachment_type ,并且attachment_type的默认值为 'Photo'
t.references :attachment, :polymorphic => {:default => 'Photo'} 
end 

以上两个方法是锦上添花之作,相当的实用.


4.在migration中使用 Model

除了使用以上方法操作数据库外,其实还可以直接在migration中使用 Model 的.比如:
Ruby代码
  1. def   self .up  
  2. #直接就用model来更新数据库,   
  3. #你可以看到 migration 一直在提供便利让你避免使用SQL,当然不是说SQL不好,只是想让你更加的统一,只要ruby就好了.     
  4. User.update_all ["receive_newsletter = ?"true ]   
  5. end    
def self.up
#直接就用model来更新数据库,
#你可以看到 migration 一直在提供便利让你避免使用SQL,当然不是说SQL不好,只是想让你更加的统一,只要ruby就好了.  
User.update_all ["receive_newsletter = ?", true] 
end 


使用model的另外一种情况是:当前migration要删除表中的一个字段 first_name,但是你的model中的某个方法使用了这个字段作为验证,比如:
Ruby代码
  1. class  User < ActiveRecord::Base  
  2.     validates_presence_of :first_name   
  3. end   
class User < ActiveRecord::Base
    validates_presence_of :first_name
end

那么当你要在migration中增加一条记录时,这个验证便不能通过,如:
Ruby代码
  1. def   self .up   
  2. User.create({:name  =>  'name' }).save   
  3. end    
def self.up 
User.create({:name => 'name'}).save 
end 

在这种情况下,你可以在migration中重新定义一个model:
Ruby代码
  1. class  XXXMigration < ActiveRecord::Migration   
  2. class  User < ActiveRecord::Base   
  3. end   
  4. def   self .up  
  5. remove_column :first_name   
  6. end   
  7.   
  8. #这个方法的作用就是取得最新的表定义   
  9. User.reset_column_information   
  10. #放心吧,这个User不会有 validates_presence_of :first_name 的验证   
  11. User.create({:name  =>  'name' }).save  
class XXXMigration < ActiveRecord::Migration 
class User < ActiveRecord::Base 
end
def self.up
remove_column :first_name
end

#这个方法的作用就是取得最新的表定义
User.reset_column_information 
#放心吧,这个User不会有 validates_presence_of :first_name 的验证
User.create({:name => 'name'}).save


=========================== migration文件的命名 =======================

按照Migration的约定去命名你的migration文件,会令你省不少功夫的,请千万要相信这一点.
如果migration文件名是这样的格式: AddXXXToYYY” or “RemoveXXXFromYYY
XXX => 字段名, YYY => 表名.
那么migration生成的时候,Rails会自动为你加上 add_column or remove_column
比如:
Ruby代码
  1. #留意类名   
  2. class  AddPartNumberToProducts < ActiveRecord::Migration   
  3. def   self .up   
  4. add_column :products:part_number:string     
  5. end     
  6. def   self .down   
  7. remove_column :products:part_number     
  8. end    
  9. end    
  10.   
  11. class  RemovePartNumberFromProducts < ActiveRecord::Migration   
  12. def   self .up   
  13. remove_column :products:part_number     
  14. end     
  15. def   self .down   
  16. add_column :products:part_number:string     
  17. end    
  18. end    
#留意类名
class AddPartNumberToProducts < ActiveRecord::Migration 
def self.up 
add_column :products, :part_number, :string  
end  
def self.down 
remove_column :products, :part_number  
end 
end 

class RemovePartNumberFromProducts < ActiveRecord::Migration 
def self.up 
remove_column :products, :part_number  
end  
def self.down 
add_column :products, :part_number, :string  
end 
end 

cool吧??

=========================== 如何执行migration =========================

执行migration的经典方法:
Ruby代码
  1. rake db :migrate   
  2.   
  3. #执行特定版本   
  4. rake db:migrate  VERSION=20080906120000   
  5.   
  6. #屏蔽migration的输出   
  7. rake db:migrate  VERBOSE= false   
  8.   
  9. #你又后悔,可以用如下方法回滚到最近的一次migration执行的状态   
  10. rake db:rollback    
  11.   
  12. #回滚到最近的3次,题外话,关于这个STEP=3到底是保存在那里的,我不曾找到,在数据库的schema_migrations表中,只有版本的信息   
  13. #莫非是按schema_migrations表记录的顺序?这个还要验证一下.   
  14. rake db:rollback  STEP=3  
rake db:migrate

#执行特定版本
rake db:migrate VERSION=20080906120000 

#屏蔽migration的输出
rake db:migrate VERBOSE=false

#你又后悔,可以用如下方法回滚到最近的一次migration执行的状态
rake db:rollback 

#回滚到最近的3次,题外话,关于这个STEP=3到底是保存在那里的,我不曾找到,在数据库的schema_migrations表中,只有版本的信息
#莫非是按schema_migrations表记录的顺序?这个还要验证一下.
rake db:rollback STEP=3


=========================== 导出migration =========================

在某些时候,你可能需要导出migration对数据库表的字义,可以导出的格式有 rb 和 sql文件两种,前一种是正宗的ruby文件,后一种是对应你使用的数据库的SQL文件.

rb:大家都知道要查看某个model的属性时,只能去db/migrate中找定义文件,但是有了这个migration的定义文件后,只要打开它就能看了.
sql:很多时候,程序员是没有权限操作数据库的,或者没有ruby环境,这时候导出的sql定义文件就有用了.

至于你需要那一种,可以配置一下 config/environment.rb
Ruby代码
  1. config.active_record.schema_format =>  :sql   # or :rb   
config.active_record.schema_format => :sql # or :rb


然后执行:
Ruby代码
  1. rake db :structure :dump   
rake db:structure:dump

到 RAILS_ROOT/db 目录下看看吧.

==================================================================

写完了,其实这里没有多少我的东西,大部分出自 http://guides.rubyonrails.org/migrations.html ,我只不过把英文变成中文罢了.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Windows上安装Ruby on Rails,你可以按照以下步骤进行操作: 1. 首先,确保你的系统已经安装了RubyRubyGems。你可以在Ruby官方网站(https://www.ruby-lang.org/)下载并安装最新版本的Ruby。 2. 打开命令提示符(Command Prompt)或PowerShell,并输入以下命令来安装Rails: ``` gem install rails ``` 这将使用RubyGems安装最新版本的Rails框架。 3. 安装完成后,你可以通过运行以下命令来验证Rails是否已成功安装: ``` rails --version ``` 如果一切顺利,你将看到Rails的版本号。 4. 接下来,你需要安装一个JavaScript运行环境。推荐使用Node.js,你可以在其官方网站(https://nodejs.org/)下载并安装最新版本的Node.js。 5. 安装完成后,你还需要安装一个数据库管理系统。Rails支持多种数据库,最常用的是MySQL和PostgreSQL。你可以选择其中一种作为你的开发环境,并安装相应的数据库软件。 - 如果选择MySQL,可以在MySQL官方网站(https://dev.mysql.com/downloads/windows/installer/)下载并安装MySQL Community Server。 - 如果选择PostgreSQL,可以在PostgreSQL官方网站(https://www.postgresql.org/download/windows/)下载并安装最新版本的PostgreSQL。 6. 安装数据库管理系统后,你还需要安装相应的Ruby库以便Rails与数据库进行交互。例如,如果你选择了MySQL,可以运行以下命令来安装相应的库: ``` gem install mysql2 ``` 如果你选择了PostgreSQL,可以运行以下命令来安装相应的库: ``` gem install pg ``` 这些命令将会使用RubyGems安装相应的库。 完成以上步骤后,你就成功在Windows上安装了Ruby on Rails。你可以开始使用Rails来开发Web应用程序了。祝你好运!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值