views:

260

answers:

4

I have two databases in use in a Ruby on Rails application; one is the database for the application while the second is an independent database over which Rails is not given control.

Problem is when loading fixtures into the dev, it tries to run DELETE statements on the tables in the independent database from the connection to the dev database, which obviously errors out.

I don't want Rails to try to do ANYTHING but read the independent database - I especially don't want it trying to delete tables.

Is there a simple way to tell Rails to ignore the models for the second database when loading fixtures?

UPDATE: To clarify, Rails seems to think the tables from the independent database are part of the development connection, though I have specified the correct connection in the model class using establish_connection. As another note, all model classes work precisely as desired from script/console.

A: 
rake db:fixtures:load RAILS_ENV=testing

will do the job for database configured as testing in your database.yml

St.Woland
I tried it with the environmental variable set using export prior to posting; per your suggestion, I also tried it this way. Same error.
Josh D Miller
You may write your own fixture to load data from *.yml to the desired database, that's all I can suggest with the info that you provide. GL
St.Woland
A: 

Okay...my issue was that I used the script/generate to create the models from the secondary database, which also created fixture, schema, test, and migrations files. I had removed the schema, test, and migrations files, but did not remove the generated fixtures (they were empty files) as I did not think it had created any.

After removing all files (including the models) from the secondary database and re-running the migrations for the dev db, I added back only the model files and the database in databases.yml from the secondary db, which solved the issue.

I still can't explain why Rails' rake tasks were looking in the wrong database and I am a little disappointed with rails' addition of the schema_migrations table in the secondary database, which it obviously does not need.

However, it works now.

Josh D Miller
A: 

Delete the model_name.yml file in your test/fixtures directory and Rails will not try to delete these tables.

Better yet, delete all your *.yml files and stop using fixtures entirely.

Luke Francl
That is what I eventually discovered from my answer below; however, it still should not have been attempting to delete them from the wrong table. I am a PHP/Python guy and I am used to model testing being much less fussy. The migrations are nice, but the rest is just a headache. I'll check out the links you provided. Thanks.
Josh D Miller
A: 

I think you might also be able to accomplish this by adding all the tables in the independent database to ActiveRecord::SchemaDumper.ignore_tables in environment.rb, like so:

ActiveRecord::SchemaDumper.ignore_tables = ['independent_db_table1', 'independent_db_table2']
Tarek