jhsu802701
jhsu802701

Reputation: 571

How do I work with a LEGACY database in Rails?

I'm working on a Rails web site that profiles stock mutual funds and ETFs. I ALREADY HAVE a separate Ruby script that runs nightly and populates a Postgres database with data on these mutual funds and ETFs.

Chapter 6 of Rails tutorial isn't quite what I'm looking for. The differences between what I'm trying to do and what chapter 6 of Rails tutorial does are: 1. In my Rails site, there is no need to create a database, because it has already been populated. So I don't think I need to use "rails generate" or "rake db:migrate". (Or am I wrong?) 2. My Rails site only reads data and does not add, delete, or edit data.

Upvotes: 1

Views: 2434

Answers (1)

Sean Hill
Sean Hill

Reputation: 15056

You don't have to create migrations. Just create your models. If the database table does not match the model name, you can use set_table_name for older versions of Rails or self.table_name = 'table_name' for newer versions. If you don't use the Rails standard for foreign keys and primary keys, you'll have to specify those as well.

For example:

# Assuming newer versions of Rails (3.2+, I believe)

class ETF < ActiveRecord::Base
  self.primary_key = 'legacy_id'
  self.table_name = 'legacy_etfs'
  has_many :closing_prices, foreign_key: 'legacy_etf_id'
end

class ClosingPrice < ActiveRecord::Base
  self.primary_key = 'legacy_id'
  self.table_name = 'legacy_closing_prices'
  belongs_to :etf, foreign_key: 'legacy_etf_id'
end

Upvotes: 2

Related Questions