Building Ruby on Rails App with an existing Mysql db

前端 未结 3 1247
栀梦
栀梦 2021-01-15 12:21

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

相关标签:
3条回答
  • 2021-01-15 12:50

    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.

    0 讨论(0)
  • 2021-01-15 12:59

    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.

    0 讨论(0)
  • 2021-01-15 13:15

    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.

    0 讨论(0)
提交回复
热议问题