rails 3 generate scaffold 会用到

阅读更多
一个可能要知道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
  end
 
  def 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
  end
 
  def self.down
    add_column :products, :part_number, :string
  end
end


class ExampleMigration < ActiveRecord::Migration
 
  def 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/

你可能感兴趣的:(Rails,Ruby,ActiveRecord,Git,ITeye)