How do I work with a LEGACY database in Rails?

北城余情 提交于 2019-12-08 04:17:40

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/16115890/how-do-i-work-with-a-legacy-database-in-rails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!