rails 3 generate scaffold 会用到
by
at 2011-06-08 22:49:32
original http://hlee.iteye.com/blog/1073636
一个可能要知道model的数据格式都支持什么
可以支持的数据类型
一个是如果要是创建完了,发现不满意怎么办呢
例如,运行如下:
rails g scaffold task project_id:integer name:string
我的话,第一想到的是git checkout或者git reset
实际上,用如下更合适
rails destroy scaffold task
那么,要是生成了scaffold并且,用了一段了时间了呢
可以先把数据库版本退回来
rake db:rollback STEP=3
当然,通常有数据就不建议rollback了,就要
就要
rails generate migration AddPartNumberToProducts part_number:string
class AddPartNumberToProducts < ActiveRecord::Migration def self.up add_column :products, :part_number, :string enddef self.down remove_column :products, :part_number end end
rails generate migration RemovePartNumberFromProducts part_number:string
class RemovePartNumberFromProducts < ActiveRecord::Migration def self.up remove_column :products, :part_number enddef self.down add_column :products, :part_number, :string end end
class ExampleMigration < ActiveRecord::Migrationdef self.up create_table :products do |t| t.references :category end #add a foreign key execute <<-SQL ALTER TABLE products ADD CONSTRAINT fk_products_categories FOREIGN KEY (category_id) REFERENCES categories(id) SQL
add_column :users, :home_page_url, :string rename_column :users, :email, :email_address
end
def self.down rename_column :users, :email_address, :email remove_column :users, :home_page_url execute "ALTER TABLE products DROP FOREIGN KEY fk_products_categories" drop_table :products end end
参考:
http://asciicasts.com/episodes/216-generators-in-rails-3
http://guides.rubyonrails.org/migrations.html
http://www.viget.com/extend/rails-3-generators-the-old-faithful/
<br><br>
<span style="color:red">
<a href="http://hlee.iteye.com/blog/1073636#comments" style="color:red">已有 <strong>1</strong> 人发表留言,猛击->><strong>这里</strong><<-参与讨论</a>
</span>
<br><br><br>
ITeye推荐