Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 889
Yurish Avatar asked Oct 23 '09 08:10

Yurish


People also ask

What does Add_index do in Rails?

It adds a multi-column index on columns one and two in resources table. The advantage of multi-column index is that it helps when you have a query with conditions on those multiple columns.

What is ActiveRecord in Ruby on Rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.


1 Answers

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
like image 195
Steve Weet Avatar answered Sep 27 '22 18:09

Steve Weet