I have a Rails project which has a Postgres database for the actual application but which needs to pull a heck of a lot of data out of an Oracle database.
database.yml looks like
development:
adapter: postgresql
database: blah blah
...
oracle_db:
adapter: oracle
database: blah blah
My models which descend from data on the Oracle DB look something like
class LegacyDataClass < ActiveRecord::Base
establish_connection "oracle_db"
set_primary_key :legacy_data_class_id
has_one :other_legacy_class, :foreign key => :other_legacy_class_id_with_funny_column_name
...
end
Now, by habit I often do a lot of my early development (and this is early development) by coding for a bit and then playing in the Rails console. For example, after defining all the associations for LegacyDataClass I'll start trying things like a = LegacyDataClass.find(:first); puts a.some_association.name
. Unexpectedly, this dies with LegacyDataClass not being already loaded.
I can then require 'LegacyDataClass'
which fixes the problem until I either need to reload!
, which won't actually reload it, or until I open a new instance of the console.
Thus the questions:
- Why does this happen? Clearly there is some Rails magic I am not understanding.
- What is the convenient Rails workaround?