Mirage
Mirage

Reputation: 1487

Building Ruby on Rails App with an existing Mysql db

I have a Mysql database with a large amount of data and tables. But i need to know how to use this Mysql data in the my new rails application. I've been learning Rails from a little time now. i know the basics of creating a scaffold which in turn creates models and controllers, but i'm unclear how to import a DB and use it. Can anyone explain or provide me with a link on how to get over this.

This is my migration after running a simple scaffold and i ran rake db:migrate after adding an email string there and nothing happened..:

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :title
      t.text :description
      t.string :image_url
      t.decimal :price, :precision => 8, :scale => 2
>>    t.string :email
      t.timestamps
    end
  end
end

Upvotes: 2

Views: 2634

Answers (3)

Tilo
Tilo

Reputation: 33732

I've done that in past projects, and it is definitely possible with Rails.

You can use legacy databases in Ruby on Rails, and you can also use multiple databases within the same Rails project.

There is a book which goes into details about how to use Rails with legacy MySQL databases, and how to tweak the models to play well with legacy tables: http://www.amazon.com/Pro-Active-Record-Databases-Experts/dp/1590598474

Using a scaffold for legacy tables will probably not get you very far, because you'll need to change a lot of the generated code to fix the naming etc..

If you also have new tables / models for your Rails application, I'd recommend to use two databases in your Rails project: one for the legacy data, and one for the new models.

Upvotes: 0

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22296

Before that, try to learn more about rails and it's conventions. Probably you'll need to adapt your database scheme. Or you could start an application and then import the data, even by SQL or by CSV. Migrating data can be a tedious work, but a necessary one.

You can check this gem to see if it helps on your case, because it will depend on your actual schema.

Or you can follow this idea and load the database schema and data from the old database. It will fail if you don't follow the rails conventions.

Upvotes: 0

nickcoxdotme
nickcoxdotme

Reputation: 6697

If you need to import that data from your current database into the one for your app, I recommend using Sequel Pro. It's a great GUI for MySQL. You can use it to export the database you want, say as a .csv file, and import it into your new database.

Regarding your migration, running a scaffold and running rake db:migrate will just set your database up; it won't add any data. So here, you've set it to create your products table, and specified the data types for its attributes.

But in order to get data in there, you can only do that via a form with your application, or by importing data.

Upvotes: 1

Related Questions