Frage

Ich habe ein Rails -Projekt, das eine Postgres -Datenbank für die tatsächliche Anwendung hat, die jedoch viele Daten aus einer Oracle -Datenbank herausholen muss.

Database.yml sieht aus

development:
  adapter: postgresql
  database: blah blah
  ...

oracle_db:
  adapter: oracle
  database: blah blah

Meine Modelle, die von Daten auf dem Oracle DB herabgehen, sehen ungefähr aus wie

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

Jetzt mache ich oft viel meiner frühen Entwicklung (und das ist früh entwickelt), indem ich ein bisschen kodiert und dann in der Rails -Konsole spiele. Zum Beispiel werde ich, nachdem ich alle Assoziationen für LegacyDataclass definiert habe a = LegacyDataClass.find(:first); puts a.some_association.name. Unerwartet stirbt dies mit LegacyDataclass, die nicht bereits geladen werden.

Ich kann dann require 'LegacyDataClass' das behebt das Problem, bis ich entweder muss reload!, was es nicht neu laden oder bis ich eine neue Instanz der Konsole öffne.

So die Fragen:

  • Warum passiert das? Offensichtlich gibt es einige Schienenmagie, die ich nicht verstehe.
  • Was ist die praktischen Schienen Problemumgehung?
War es hilfreich?

Lösung

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.

Andere Tipps

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top