简体   繁体   中英

Model from existing table in Rails 2

I have a database with tables. I want to create a model in my Rails app from existing table. As i know, such functionality is available, and is done as follows:

script/generate scaffold model_name --skip-migration


Of course, i defined my database in database.yml file. Scaffold generated for me a model with controller and views. My table name is not as it must be for Rails(it is incorrect, not following conventions), i added set_table_name to my controller. But, when i am calling the index method, on my page i have only set of # symbols, but not a data from database. In my index.html.erb i have only generated code by scaffold. How can i print out my database data?

Have you generated a schema file from your existing database? If you run the command

rake db:schema:dump

and then re-generate your scaffold this should fix the problem.

Additionally you may wish to check out Dr Nic's Magic Model generator . This will generate models for all of your existing tables and attempt to guess the relationships. This will probably not work if your table naming is not understandable by rails.

UPDATE

I do not generally use the default scaffold however I have tested this myself and it appears that if you skip the migration and do not pass any column name/type pairs then the scaffold generator will not create anything in the template to render the columns.

You have two choices here either

  1. Pass in the column name pairs as well as skip-migration or
  2. Download Ryan Bates Nifty Scaffold generator which will create the scaffold with the column names even if you specify --skip-migration

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