Model from an existing table in Rails 2

I have a database with tables. I want to create a model in a Rails application from an existing table. As I know, this functionality is available and is implemented as follows: Of course, I defined my database in the database.yml file. Scaffold created a model for me with a controller and views. My table name is not what it should be for Rails (this is wrong, not the following conventions), I added set_table_name to my controller. But, when I call the index method, on my page I only have the # character set, but not the data from the database. In my index.html.erb, I only generated the code using the scaffold. How can I print my database data?

script/generate scaffold model_name --skip-migration



+5
source share
1 answer

Have you created a schema file from an existing database? If you run the command

rake db:schema:dump

and then recreate your scaffold, this should fix the problem.

Alternatively, you can check out the Dr Nic Magic Model generator . This will create models for all of your existing tables and try to guess the relationship. This probably won't work if your table name is not clear with rails.

UPDATE

Usually I don’t use the default sketch, but I tested it myself, and it seems that if you skip the migration and do not go through any pairs of column names / types, the scaffold generator will not create anything in the template to display the columns.

Here you have two options:

  • Go into the column name pairs and also skip the migration or
  • Ryan Bates Nifty Scaffold, , --skip-migration
+7

All Articles