本文介绍了如何在 Rails 中使用 LEGACY 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个介绍股票共同基金和 ETF 的 Rails 网站.我已经有一个单独的 Ruby 脚本,它每晚运行,并用这些共同基金和 ETF 的数据填充 Postgres 数据库.

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.

Rails 教程的第 6 章并不是我想要的.我正在尝试做的事情与 Rails 教程第 6 章所做的事情之间的区别是:1.在我的Rails站点中,不需要创建数据库,因为它已经被填充了.所以我认为我不需要使用rails generate"或rake db:migrate".(还是我错了?)2.我的Rails站点只读取数据,不增删改数据.

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.

推荐答案

您不必创建迁移.只需创建您的模型.如果数据库表与模型名称不匹配,您可以将 set_table_name 用于旧版本的 Rails 或 self.table_name = 'table_name' 用于较新版本.如果您不对外键和主键使用 Rails 标准,则还必须指定它们.

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.

例如:

# 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

这篇关于如何在 Rails 中使用 LEGACY 数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 03:37