views:

390

answers:

2

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?
+2  A: 

I believe this might have to do with your model name, rather than your connection. The Rails convention is that model class names are CamelCase, while the files they reside in are lowercase+underscore.

The "LegacyModel" class should therefore be in models/legacy_model.rb. Your statement about "require 'LegacyDataClass'" indicates that this is not the case, and therefore Rails doesn't know how to automagically load that model.

James Baker
Gaaaaaaaah, Can you tell who does too much Java for his own good? I didn't use the generators because I figured I didn't need the autogenerated migrations, and then I applied the Java "filename = classname + extension" convention without thinking about it.Thanks a million!
Patrick McKenzie
A: 

I wrote something for an app at work that handles connections to other databases' at runtime, it might be able to help.

http://github.com/cherring/connection_ninja

railsninja