1

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.

jhsu802701
  • 573
  • 1
  • 7
  • 23
  • 1
    Have a look at this question http://stackoverflow.com/questions/1226182/how-do-i-work-with-two-different-databases-in-rails-with-active-records – mind.blank Apr 20 '13 at 02:30
  • 2
    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`. If you don't use the Rails standard for foreign keys, you'll have to specify the foreign key in the relationships. – Sean Hill Apr 20 '13 at 02:30
  • 1
    @mind.blank, that question is more about connecting to multiple DBs within an app. – Sean Hill Apr 20 '13 at 02:31
  • 1
    @SeanHill you should add your comment as an answer – max Sep 30 '14 at 19:34
  • Done. Thanks... I had forgotten about this question. :-) – Sean Hill Sep 30 '14 at 19:52

1 Answers1

2

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
Sean Hill
  • 14,978
  • 2
  • 50
  • 56
  • 1
    If you have 30+ models, that's a lot of typing You might want to check out the "schema to scaffold" gem. https://github.com/frenesim/schema_to_scaffold - scaffolds create migrations, controllers, models, and views. Validations, relationships, and 'non-rails-named' tables and keys, are defined in your models automatically. – JosephK Apr 18 '16 at 15:54