简体   繁体   中英

Rails generate scaffold from migration file

I created a Rails 4 migration file using:

rails g migration CreateCompanyAndAttributes

I edited the migration to be:

def change

  create_table :companies do |c|
    c.integer :name
    c.string :logo_url
    c.timestamps
  end

  create_table :attributes do |a|
    a.string :name
    a.string :description
    a.string :image
    a.timestamps
  end

  create_table :company_attributes do |t|
    t.integer :facility_id
    t.integer :attribute_id
    t.timestamps
  end

end 

Now that my migration is ready to be deployed, how can I generate scaffold for all three soon to be created tables?

Should I first run:

rake db:migrate

Then something like

rails g scaffold companies
rails g scaffold attributes
rails g scaffold companies_attributes

It seems that you want to have the scaffold without the migration (you have already done it manually)

You can run the scaffold command with Use the --skip-migration flag. For example:

rails g scaffold Company name:string logo_url:string --skip-migration

Hope it meets your need! :)

Since I was not able to find an answer to my original question starting with a migration file, I ended up deleting the migration file and used separate rails generate scaffold command line commands for each table.

rails g scaffold Company name:string logo_url:string
rails g scaffold Attribute name:string description:string image:string
rails g scaffold CompanyAttribute company_id:integer attribute_id:integer
rake db:migrate

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM