24

I have an application written in PHP/MySQL (symfony, to be specific) that I'd (potentially) like to rewrite in Rails. I know how to create scaffolding for tables that don't exist yet, but how do I get Rails to read my existing table structure and create scaffolding based on that?

Update: it turns out I can run the following command to get Rails to generate models for me:

rails generate scaffold Bank --no-migration

But it doesn't give me forms. I would prefer something that gives me forms.

Jason Swett
  • 43,526
  • 67
  • 220
  • 351

2 Answers2

29

The answer is db:schema:dump.

http://guides.rubyonrails.org/migrations.html

Jason Swett
  • 43,526
  • 67
  • 220
  • 351
  • And then create models proper for those of them where you want to use "forms". – Krule Jan 14 '11 at 15:59
  • Oh yeah, forgot about the forms. Do I have to create those models manually? – Jason Swett Jan 14 '11 at 16:08
  • You, should create models and define relations in them, then create forms where you need them. – Krule Jan 15 '11 at 12:12
  • 2
    also this tutorial: http://blog.joelberghoff.com/2013/02/06/ruby-on-rails-tutorial-creating-a-rails-instance-from-an-existing-mysql-db/ – s2t2 Jul 28 '13 at 02:06
  • @s2t2 could you explain the reasoning behind adding a migration as suggested in the blog? Will that work nicely with existing db? Shouldnt `db:create` be used instead? – Karthik T May 17 '14 at 17:32
  • Yup, migration is a bad idea, cleared out my data when i tried to migrate – Karthik T May 18 '14 at 03:08
12

The easiest route is to pretend that you are writing a fresh app with a similar database schema - you can then create your models and migrations with the old schema in mind, but without being restricted by it. At a later stage, you can create a database migration script to copy all the old data into the new schema.

I'm doing this right now. The benefit of this approach is that you can take advantage of all of the rapid development tools and techniques provided by Rails (including scaffolds) without being slowed by trying to retrofit to the exact same schema.

However, if you do decide that you don't like this approach, and you do need to map your new models to existing tables, there are a number of configuration options provided by active record where you can override the convention over configuration naming patterns and map model names to tables names, set oddly named ID fields etc. For example:

class Mammals < ActiveRecord::Base
  set_table_name "tbl_Squirrels"
  set_primary_key :squirrel_id
end

The above will help Rails attempt to read your existing table, but success will depend upon how well the existing table structures matches Rails conventions. You may have to supply more configuration information to get it to work, and even then it might not work.

Finally, it may be worth considering the use of DataMapper which I believe is more suited to existing brownfield databases than ActiveRecord, because it allows you to map everything, but of course you will need to learn that API if you don't already know it.

Scott
  • 17,127
  • 5
  • 53
  • 64